Translate c++ code to java

asked 2014-05-29 09:06:45 -0500

iulianrosca gravatar image

updated 2014-05-29 09:10:43 -0500

Hello, I'm trying to implement the following algorithm for rectangles detection in JAVA:

Mat im = imread(inputFileName,CV_LOAD_IMAGE_GRAYSCALE);
Mat outputIm(im.rows,im.cols,CV_8U, Scalar(0));
Mat bi;
// step 1: adaptive thresholding 
adaptiveThreshold(im,bi,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY,7,50);

threshold(bi, bi, 128.0, 255.0, CV_THRESH_BINARY_INV);


    // step 2: connected component analysis
std::vector<std::vector<cv::Point> > contours;

findContours(bi, contours, CV_RETR_EXTERNAL , CV_CHAIN_APPROX_NONE);  

    // step 3: analyze these blobs
double area;
std::vector<double> areaArray;
for(int i=0; i<contours.size(); i++)
{
    cv::Rect rect = boundingRect(contours[i]);
    area = rect.width;
    areaArray.push_back(area);
}
std::vector<double> sortedAreaArray;
sortedAreaArray = areaArray;
size_t n = sortedAreaArray.size() / 2;
    nth_element(sortedAreaArray.begin(), sortedAreaArray.begin()+n, sortedAreaArray.end());

double medianArea = sortedAreaArray[n];

for(int i=0; i<contours.size(); i++)
{
    if(areaArray[i]>5*medianArea)
    {


         for(int j=0; j<contours[i].size(); j++)
            {
                int x = contours[i][j].x;
                int y = contours[i][j].y;
                int pos = x+y*bi.cols;
                outputIm.data[pos]=255;
            }
        }

    }
    imwrite(outputFileName,outputIm);

I have problems with translating this part: how can I retrieve x and y from a java Mat class? and there is no .data() for java, how can I set individual pixels?

 for(int j=0; j<contours[i].size(); j++)
                {
                    int x = contours[i][j].x;
                    int y = contours[i][j].y;
                    int pos = x+y*bi.cols;
                    outputIm.data[pos]=255;
                }
edit retag flag offensive close merge delete

Comments

1

the docs will be quite helpful then .

http://docs.opencv.org/java/org/opencv/core/Mat.html#put(int,%20int,%20byte[])

but you probably don't even need to. there's Imgproc.drawContours() as well.

berak gravatar imageberak ( 2014-05-29 09:16:48 -0500 )edit