我尝试使用opencv在照片中找到红色。然而,它每次都会结束一张完全黑的照片。
我在图片中找到了所有的红色,蓝色和绿色。然后,我将三个img分为绿色,红色和蓝色。
Down是我的代码,
Mat mat1 = new Mat();
mat1 = Highgui.imread("test2.jpg");
int h = mat1.height();
int w = mat1.width();
Mat temp8u = new Mat(h, w, CvType.CV_8UC1);
Mat imgR = new Mat(h, w, CvType.CV_32FC1);
Mat imgG = new Mat(h, w, CvType.CV_32FC1);
Mat imgB = new Mat(h, w, CvType.CV_32FC1);
Mat temp = new Mat(h, w, CvType.CV_32FC1);
Mat temp1 = new Mat(h, w, CvType.CV_32FC1);
Mat temp2 = new Mat(h, w, CvType.CV_32FC1);
Mat temp3 = new Mat(h, w, CvType.CV_32FC1);
Mat temp4 = new Mat(h, w, CvType.CV_32FC1);
List<Mat> listImg = new ArrayList<Mat>(3);
Core.split(mat1, listImg);
Mat imgR8u = listImg.get(0);
Mat imgG8u = listImg.get(1);
Mat imgB8u = listImg.get(2);
imgR8u.convertTo(imgR, CvType.CV_32FC1);
imgG8u.convertTo(imgG, CvType.CV_32FC1);
imgB8u.convertTo(imgB, CvType.CV_32FC1);
int size = (int) (imgR.total() * imgR.channels()); //方法2
float[] buff = new float[size];
imgR.get(0, 0, buff);
for (int i = 0; i < size; i++)
if (buff[i] > 200)
buff[i] = (float) 255;
else
buff[i] = (float) 0;
//buff[i] = (byte) (255 - buff[i]);
temp1.put(0, 0, buff);
Core.add(imgR,imgG,temp);
Core.add(temp,imgB,temp);
Core.add(temp, new Scalar(1,1,1), temp);
Core.divide(imgR,temp,temp2);
Imgproc.threshold(temp2, temp2,0.37,255,Imgproc.THRESH_BINARY);
Core.divide(imgG,temp,temp3);
Imgproc.threshold(temp3,temp3,0.30,255,Imgproc.THRESH_BINARY_INV);
Core.divide(imgB,temp,temp4);
Imgproc.threshold(temp4,temp4,0.32,255,Imgproc.THRESH_BINARY_INV);
Core.bitwise_and(temp1,temp2,temp2);
Core.bitwise_and(temp2,temp3,temp3);
Core.bitwise_and(temp3,temp4,temp);
temp.convertTo(temp8u, CvType.CV_8UC1);
Highgui.imwrite("Threshold.jpg", temp8u);
为什么我的最终照片Threshold.jpg都是黑色的?