1 | initial version |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here, but here is the java version:
public static void main(String[] args) {
Mat m = ... your stuff here. I suppose a CV_U8 matrix
int size = (int)m.total();
int nbChannels = m.channels();
byte[] temp = new byte[size * nbChannels];
m.get(0, 0, temp);
Set<String> set = new HashSet<Integer>();//will contain all different values
for (int i = 0; i < size; i++)
{
int pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] << (8*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
2 | No.2 Revision |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here, here so I can't test if the solution I suggest work, but here is the java version:idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = ... your stuff here. I suppose a CV_U8 matrix
int size = (int)m.total();
int nbChannels = m.channels();
byte[] temp = new byte[size * nbChannels];
m.get(0, 0, temp);
Set<String> set = new HashSet<Integer>();//will contain all different values
for (int i = 0; i < size; i++)
{
int pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] << (8*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
3 | No.3 Revision |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest work, is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = ... your stuff here. I suppose a CV_U8 matrix
int size = (int)m.total();
int nbChannels = m.channels();
byte[] temp = new byte[size * nbChannels];
m.get(0, 0, temp);
Set<String> set = new HashSet<Integer>();//will contain all different values
for (int i = 0; i < size; i++)
{
int pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] << (8*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
4 | made a quick test: http://sugarcoatedchili.herokuapp.com/share/33605 hope you don't mind the edit ;) |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = ... your stuff here. I suppose a CV_U8 matrix
int size = (int)m.total();
int nbChannels = m.channels();
byte[] temp = new byte[size * nbChannels];
m.get(0, 0, temp);
Set<String> Set<Integer> set = new HashSet<Integer>();//will contain all different values
for (int i = 0; i < size; i++)
{
int pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] << (8*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
5 | No.5 Revision |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = ... your stuff image here. I suppose a CV_U8 matrix
Mat dblImg;//use double as unsigned byte doesn't exist in java...
m.convertTo(dblImg, CvType.CV_64FC3);
int size = (int)m.total();
int nbChannels = m.channels();
byte[] double[] temp = new byte[size double[size * nbChannels];
m.get(0, dblImg.get(0, 0, temp);
Set<Integer> Set<Double> set = new HashSet<Integer>();//will HashSet<Double>();//will contain all different values
for (int i = 0; i < size; i++)
{
int double pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] << (8*j));
* (1.+255.*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
6 | No.6 Revision |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = ... //... your image here. I suppose a CV_U8 matrix
here.
Mat dblImg;//use double as unsigned byte doesn't exist in java...
if(m.channels()==3)
m.convertTo(dblImg, CvType.CV_64FC3);
else
m.convertTo(dblImg, CvType.CV_64FC1);
int size = (int)m.total();
int nbChannels = m.channels();
double[] temp = new double[size * nbChannels];
dblImg.get(0, 0, temp);
Set<Double> set = new HashSet<Double>();//will contain all different values
for (int i = 0; i < size; i++)
{
double pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] * (1.+255.*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
7 | No.7 Revision |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = //... your image here.
Mat dblImg;//use dblImg = new Mat();//use double as unsigned byte doesn't exist in java...
if(m.channels()==3)
m.convertTo(dblImg, CvType.CV_64FC3);
else
m.convertTo(dblImg, CvType.CV_64FC1);
int size = (int)m.total();
int nbChannels = m.channels();
double[] temp = new double[size * nbChannels];
dblImg.get(0, 0, temp);
Set<Double> set = new HashSet<Double>();//will contain all different values
for (int i = 0; i < size; i++)
{
double pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] * (1.+255.*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
8 | No.8 Revision |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = //... your image here.
Mat dblImg = new Mat();//use double as unsigned byte doesn't exist in java...
if(m.channels()==3)
m.convertTo(dblImg, CvType.CV_64FC3);
else
m.convertTo(dblImg, CvType.CV_64FC1);
int size = (int)m.total();
int nbChannels = m.channels();
double[] temp = new double[size * nbChannels];
dblImg.get(0, 0, temp);
Set<Double> set = new HashSet<Double>();//will contain all different values
for (int i = 0; i < size; i++)
{
double pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] * (1.+255.*j));
(1.+256.*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
9 | fixed overlapping in pixel val computation |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = //... your image here.
Mat dblImg = new Mat();//use double as unsigned byte doesn't exist in java...
if(m.channels()==3)
m.convertTo(dblImg, CvType.CV_64FC3);
else
m.convertTo(dblImg, CvType.CV_64FC1);
int size = (int)m.total();
int nbChannels = m.channels();
double[] temp = new double[size * nbChannels];
dblImg.get(0, 0, temp);
Set<Double> set = new HashSet<Double>();//will contain all different values
for (int i = 0; i < size; i++)
{
double pixelVal = 0;
temp[i*nbChannels];
for(int j=0; j=1; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] * (1.+256.*j));
256. * j);
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
I hope this will solve your problem!
10 | No.10 Revision |
Hi costa_974!
The problem you have is not that simple : color is something very hard to define ;-) But if you just want to count the different values your Mat contains, the best you can do is to follow the C# example. I don't have opencv java-binding enabled here so I can't test if the solution I suggest is correct, but here is the idea:
import java.util.HashSet;
public static void main(String[] args) {
Mat m = ... your stuff here. I suppose a CV_U8 matrix
int size = (int)m.total();
int nbChannels = m.channels();
byte[] temp = new byte[size * nbChannels];
m.get(0, 0, temp);
Set<Integer> set = new HashSet<Integer>();//will contain all different values
for (int i = 0; i < size; i++)
{
int pixelVal = 0;
for(int j=0; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] << (8*j));
set.add(pixelVal);
}
System.out.println("Color number : " + set.size());
}
An other version (but doesn't seems to work) using double conversion in order to deal with unsigned char matrix...
import java.util.HashSet;
public static void main(String[] args) {
Mat m = //... your image here.
Mat dblImg = new Mat();//use double as unsigned byte doesn't exist in java...
if(m.channels()==3)
m.convertTo(dblImg, CvType.CV_64FC3);
else
m.convertTo(dblImg, CvType.CV_64FC1);
int size = (int)m.total();
int nbChannels = m.channels();
double[] temp = new double[size * nbChannels];
dblImg.get(0, 0, temp);
Set<Double> set = new HashSet<Double>();//will contain all different values
for (int i = 0; i < size; i++)
{
double pixelVal = temp[i*nbChannels];
for(int j=1; j<nbChannels; j++)
pixelVal+= (temp[i*nbChannels+j] * 256. * j);
set.add(pixelVal);
}
System.out.println("Color number : " + set.size()) ;
}
}
I hope this will solve your problem!