在OpenCV4Android中,我可以从Mat中删除一些特定的像素吗?

时间:2016-01-08 16:28:45

标签: android opencv opencv3.0 mat opencv4android

例如,假设我要从Mat中删除位于(50, 50)的像素。我可以删除吗?

某些背景信息:

我有一个包含轮廓的Mat。我想要做的是迭代Mat中的每个像素和我想删除不在轮廓内的每个像素,这样我只剩下部分的位于轮廓内的Mat这可能吗?

所以我在做

for (int firstCoordinate = 0; firstCoordinate < hsvMat.rows(); firstCoordinate++) {//This and nested for are used to iterate through the Mat
    for (int secondCoordinate = 0; secondCoordinate < hsvMat.cols(); secondCoordinate++) {
        if (Imgproc.pointPolygonTest(contour, new Point(firstCoordinate, secondCoordinate), false) < 0) {//If this point is outside the contour. 
            //***HERE:I want to remove this pixel. 
           //****SOMETHING LIKE:
           //***hsvMat.get(firstCoordinate,secondCoordinate).remove();
        }
    }
}

修改

这是捕获的初始RGBA帧:

enter image description here

请注意,显示为蓝色的圆圈实际上是红色(RGB,BGR问题)。

通过Imgproc.cvtColor(rgbaFrame, hsvImage, Imgproc.COLOR_RGB2HSV);,以下是HSV图像。

enter image description here

然后通过Core.inRange(hsvImage, new Scalar(0, 50, 40), new Scalar(10, 255, 255), maskedImage);,然后Imgproc.dilate(maskedImage, dilatedMat, new Mat());,我们得到

enter image description here enter image description here

然后通过

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(dilatedMat, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
        // \/ We will 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(i);
            double currentContourArea = Imgproc.contourArea(currentContour);
            if (currentContourArea > largestContourArea) {
                largestContourArea = currentContourArea;
                largestContour = currentContour;
            }
        }
Rect detectedBlobRoi = Imgproc.boundingRect(largestContour);
Mat detectedBlobRgba = rgbaFrame.submat(detectedBlobRoi);

我们得到了

enter image description here

然后通过Imgproc.cvtColor(detectedBlobRgba, detectedBlobHsv, Imgproc.COLOR_RGB2HSV);,我们得到

enter image description here

然后当我做

//CALCULATING THE AVERAGE COLOR OF THE DETECTED BLOB
int pixelsRemoved = 0;
for (int firstCoordinate = 0; firstCoordinate < detectedBlobHsv.rows(); firstCoordinate++) {
    for (int secondCoordinate = 0; secondCoordinate < detectedBlobHsv.cols(); secondCoordinate++) {
        if (Imgproc.pointPolygonTest(largestContour2f,new Point(firstCoordinate, secondCoordinate),false) == -1) {
            pixelsRemoved++;
            detectedBlobHsv.put(firstCoordinate, secondCoordinate, new double[]{0,0,0});
        }
    }
}

它出错了。这是我将detectedBlobHsv写入SD卡时所得到的(左边只有一条带红色的垂直线= /)

enter image description here

0 个答案:

没有答案