I am trying to convert a method from c++ to java. Here is the method:
void rgb2cmyk(cv::Mat& src, std::vector<cv::Mat>& cmyk)
{
CV_Assert(src.type() == CV_8UC3);
cmyk.clear();
for (int i = 0; i < 4; ++i)
cmyk.push_back(cv::Mat(src.size(), CV_32F));
for (int i = 0; i < src.rows; ++i)
{
for (int j = 0; j < src.cols; ++j)
{
cv::Vec3b p = src.at<cv::Vec3b>(i,j);
float r = p[2] / 255.;
float g = p[1] / 255.;
float b = p[0] / 255.;
float k = (1 - std::max(std::max(r,g),b));
cmyk[0].at<float>(i,j) = (1 - r - k) / (1 - k);
cmyk[1].at<float>(i,j) = (1 - g - k) / (1 - k);
cmyk[2].at<float>(i,j) = (1 - b - k) / (1 - k);
cmyk[3].at<float>(i,j) = k;
}
}
}
Problem is with the methods of OpenCv. Here is some detail:
CV_Assert
method in java. dont know any alternate
for that.cmyk.push_back
is replaced with cmyk[i].pushback
Mat
replacing cv::Vec3b
, it is shows no errorstd::max
is replaced with Math.max
cmyk[0].at<float>(i,j)
Do any one have suggestion or any better approach of changing this method to java.
Thanks in advance for help....
Edit
What i did
public void rgb2xmyk(Mat src,Mat[] cmyk)
{
//CV_Assert(src.type() == CV_8UC3);
//cmyk.clear();
for (int i = 0; i < 4; ++i)
cmyk[i].push_back(new Mat(src.size(), CvType.CV_32F));
for (int i = 0; i < src.rows; ++i)
{
for (int j = 0; j < src.cols; ++j)
{
double[] p = src.get(i,j);
float r = (float) (p[2] / 255.);
float g = (float) (p[1] / 255.);
float b = (float) (p[0] / 255.);
float k = (1 - Math.max(Math.max(r,g),b));
cmyk[0].at<float>(i,j) = (1 - r - k) / (1 - k);
cmyk[1].at<float>(i,j) = (1 - g - k) / (1 - k);
cmyk[2].at<float>(i,j) = (1 - b - k) / (1 - k);
cmyk[3].at<float>(i,j) = k;
}
}
}
You must make sure that cmyk array of Mat has size=4. In the for loop, I suggest you use setTo:
for (int i = 0; i < 4; ++i)
cmyk[i].setTo(new Mat(src.size(), CvType.CV_32F));
in the nested for loops, where you fill your cmyk, I would use put method
cmyk[0].put(i,j,new Float[] {(1 - r - k) / (1 - k)});
cmyk[1].put(i,j,new Float[] {(1 - g - k) / (1 - k)});
cmyk[2].put(i,j,new Float[] {(1 - b - k) / (1 - k)});
cmyk[3].put(i,j,new Float[] {k});
...hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With