4

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

Community
  • 1
  • 1
birdy
  • 9,286
  • 24
  • 107
  • 171
  • Like I told you before, use JavaCV. – syb0rg Mar 30 '13 at 17:50
  • @syb0rg It looks like you deleted your answer before. Don't get spooked by a downvote. It comes with the territory. – Drew Dormann Mar 30 '13 at 17:52
  • @DrewDormann I didn't get spooked, it got down-voted and another answer was accepted, meaning the OP didn't listen to my advice. It would make their life a lot easier if they would though... – syb0rg Mar 30 '13 at 17:55
  • @syb0rg I tried gown down the route of JavaCV however, it doesn't have some methods I want. for example `getStructuringElement`. Maybe they have a different name for that method but i was not able to find it. After going through that pain I decided to give OpenCV java bindings a try since they have a 1 to 1 matching with C functions at least. – birdy Mar 30 '13 at 17:59

1 Answers1

4

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); 
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • @karlphillip can you check this question http://stackoverflow.com/questions/29204124/how-to-iterate-in-opencv4android similar to this one. – Ashok Varma Mar 23 '15 at 06:08