Looping through OpenCV Mat in Java bindings

放肆的年华 提交于 2019-12-18 17:13:29

问题


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..


回答1:


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); 


来源:https://stackoverflow.com/questions/15721550/looping-through-opencv-mat-in-java-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!