我是 OpenCV4Android 的新手。这是我编写的一些代码,用于检测图像中的蓝色斑点。在以下图片中,图片 1 在我的笔记本电脑中。我运行应用程序,OpenCV 相机捕获的帧是图像 2。您可以查看代码以查看其余图像是什么。(正如您在代码中看到的,所有图像都保存在 SD 卡中。)
我有以下问题:。
为什么在相机捕获的 rgba 帧中,浅蓝色斑点的颜色变成了浅黄色(如图 2 所示)。
boundingRect
我围绕最大的蓝色 blob创建了一个,但随后ROI
通过rgbaFrame.submat(detectedBlobRoi)
. 但你可以在最后一张图片中看到,它看起来就像几个灰色像素。我期待蓝色球体与图像的其余部分分开。
我错过了什么或做错了什么?
代码:
private void detectColoredBlob () {
Highgui.imwrite("/mnt/sdcard/DCIM/rgbaFrame.jpg", rgbaFrame);//check
Mat hsvImage = new Mat();
Imgproc.cvtColor(rgbaFrame, hsvImage, Imgproc.COLOR_RGB2HSV_FULL);
Highgui.imwrite("/mnt/sdcard/DCIM/hsvImage.jpg", hsvImage);//check
Mat maskedImage = new Mat();
Scalar lowerThreshold = new Scalar(170, 0, 0);
Scalar upperThreshold = new Scalar(270, 255, 255);
Core.inRange(hsvImage, lowerThreshold, upperThreshold, maskedImage);
Highgui.imwrite("/mnt/sdcard/DCIM/maskedImage.jpg", maskedImage);//check
Mat dilatedMat= new Mat();
Imgproc.dilate(maskedImage, dilatedMat, new Mat() );
Highgui.imwrite("/mnt/sdcard/DCIM/dilatedMat.jpg", dilatedMat);//check
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(dilatedMat, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
//Use only the largest contour. Other contours (any other possible blobs of this color range) will be ignored.
MatOfPoint largestContour = contours.get(0);
double largestContourArea = Imgproc.contourArea(largestContour);
for ( int i=1; i<contours.size(); ++i) {//NB Notice the prefix increment.
MatOfPoint currentContour = contours.get(0);
double currentContourArea = Imgproc.contourArea(currentContour);
if (currentContourArea > largestContourArea) {
largestContourArea = currentContourArea;
largestContour = currentContour;
}
}
Rect detectedBlobRoi = Imgproc.boundingRect(largestContour);
Mat detectedBlobRgba = rgbaFrame.submat(detectedBlobRoi);
Highgui.imwrite("/mnt/sdcard/DCIM/detectedBlobRgba.jpg", detectedBlobRgba);//check
}
- 电脑中的原始图像,这是通过将手机的摄像头放在笔记本电脑屏幕前拍摄的。
- rgbaFrame.jpg
- hsvImage.jpg
- 扩张图像.jpg
- 蒙面垫.jpg
- 检测到的BlobRgba.jpg
编辑:
我刚刚使用Core.inRange(hsvImage, new Scalar(0,50,40), new Scalar(10,255,255), maskedImage);//3, 217, 225 --- 6, 85.09, 88.24 ...... 3 219 255
了,我通过给它一个自定义的红色 HSV 值来捕获网站 colorizer.org 的截图,即对于 OpenCV 红色Scalar(3, 217, 255)
(它落在给定inRange
函数中设置的范围内,我将通道值缩放到colorizer.org 的比例,即 H=0-360,S=0-100,V=0-100,通过将 H 值乘以 2,并将 S 和 V 值除以 255 并乘以 100。这给出了我6, 85.09, 88.24
在网站上设置,并截取了屏幕截图(下图中的第一个)。
- 原始截图,我捕捉到了这一帧。
- rgbaFrame.jpg
- hsvImage.jpg
- 蒙面图像.jpg
- 膨胀垫.jpg
- 检测到的BlobRgba.jpg
重要的:
当我触摸 rgbaFrame 内部时(即在onTouch
方法内部调用它),给定的方法实际上是在我的测试应用程序中调用的。我还使用以下代码打印到我触摸过的彩色 blobTextView
的Hue
、Saturation
和值。当我运行这个应用程序时,我触摸了红色的 blob,并得到了以下值:.Value
Hue:3, Saturation:219, Value:255
public boolean onTouch(View v, MotionEvent motionEvent) {detectColoredBlob(); int cols = rgbaFrame.cols(); int 行 = rgbaFrame.rows();
int xOffset = (openCvCameraBridge.getWidth() - cols) / 2;
int yOffset = (openCvCameraBridge.getHeight() - rows) / 2;
int x = (int) motionEvent.getX() - xOffset;
int y = (int) motionEvent.getY() - yOffset;
Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")");//check
if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) { return false; }
Rect touchedRect = new Rect();
touchedRect.x = (x > 4) ? x - 4 : 0;
touchedRect.y = (y > 4) ? y - 4 : 0;
touchedRect.width = (x + 4 < cols) ? x + 4 - touchedRect.x : cols - touchedRect.x;
touchedRect.height = (y + 4 < rows) ? y + 4 - touchedRect.y : rows - touchedRect.y;
Mat touchedRegionRgba = rgbaFrame.submat(touchedRect);
Mat touchedRegionHsv = new Mat();
Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);
double[] channelsDoubleArray = touchedRegionHsv.get(0, 0);//**********
float[] channelsFloatArrayScaled = new float[3];
for (int i = 0; i < channelsDoubleArray.length; i++) {
if (i == 0) {
channelsFloatArrayScaled[i] = ((float) channelsDoubleArray[i]) * 2;// TODO Wrap an ArrayIndexOutOfBoundsException wrapper
} else if (i == 1 || i == 2) {
channelsFloatArrayScaled[i] = ((float) channelsDoubleArray[i]) / 255;// TODO Wrap an ArrayIndexOutOfBoundsException wrapper
}
}
int androidColor = Color.HSVToColor(channelsFloatArrayScaled);
view.setBackgroundColor(androidColor);
textView.setText("Hue : " + channelsDoubleArray[0] + "\nSaturation : " + channelsDoubleArray[1] + "\nValue : "
+ channelsDoubleArray[2]);
touchedRegionHsv.release();
return false; // don't need subsequent touch events
}