Search code examples
javac++opencvimage-processingjavacv

Looping through OpenCV Mat in Java bindings


I am trying to convert a C++ method from a previous answer I received using OpenCV to Java using OpenCV Java bindings

C++ code:

cv::Mat gray;
cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS,
                                       cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),
                                       cv::Point(erosion_size, erosion_size) );
cv::erode(gray, gray, element);
 // Scan the image searching for points and store them in a vector
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = gray.begin<uchar>();
cv::Mat_<uchar>::iterator end = gray.end<uchar>();
for (; it != end; it++)
{
    if (*it) 
        points.push_back(it.pos()); 
}

I can't figure out how to loop through the gray variable as they are in C code. Below is what I've got so far:

Java Code:

Mat gray = new Mat();
Mat element = Imgproc.getStructuringElement(
                Imgproc.MORPH_CROSS, new Size(2 * erosion_size + 1, 2 * erosion_size + 1), new Point(erosion_size, erosion_size)
        );
Imgproc.erode(img, gray, element);
List <Point> p = new ArrayList<Point>();
//How to loop through the gray variable?

I went through the API's and it seems that Mat does have methods for getting rows and cols but I can't call Iterator on anything..


Solution

  • If the Mat is truly gray, it's probably of type CV_8U:

    for (int i = 0; i < gray.rows; i++)
    {
        for (int j = 0; j < gray.cols; j++)
        {
            byte pixel = gray.get(i, j);
        }
    }
    

    I don't have my environment setup to test this code. For more info check this thread.

    If you are using JavaCV, some time ago I wrote a code to iterate on the pixels of a colored image and convert it to grayscale like this:

        IplImage imageSrc = cvLoadImage("pipeline.png", CV_LOAD_IMAGE_COLOR);
        if (imageSrc == null) 
        {
            System.out.println("!!! Failed loading image");
            return;
        }
    
        double r, g, b;
        r = g = b = 0.0;               
    
        IplImage imageDst = IplImage.create(imageSrc.width(), imageSrc.height(), IPL_DEPTH_8U, 3);
    
        CvMat imagePixels = CvMat.createHeader(imageDst.height(), imageDst.width(), CV_32FC1);  
        cvGetMat(imageDst, imagePixels, null, 0); 
        int x, y;
        for(y=0; y<imageSrc.height(); y++)
           for(x=0; x<imageSrc.width(); x++)
           {
                CvScalar rgb = cvGet2D(imagePixels, y, x);
                r = rgb.val(0);
                g = rgb.val(2);
                b = rgb.val(1);
    
                double gray = (r + g + b) / 3;
    
                CvScalar scalar = new CvScalar();
                scalar.setVal(0, gray);
                scalar.setVal(1, gray);
                scalar.setVal(2, gray);
               cvSet2D(imagePixels, y, x, scalar);
           }
           imageDst = new IplImage(imagePixels);  
    
           IplImage result = new IplImage(imagePixels); 
           cvSaveImage("manual_gray.png", result);
    
           cvReleaseImage(imageSrc);