Ask Your Question

iulianrosca's profile - activity

2020-10-23 05:27:51 -0600 received badge  Student (source)
2017-05-27 08:22:17 -0600 received badge  Famous Question (source)
2016-11-01 23:52:05 -0600 received badge  Notable Question (source)
2016-04-25 05:08:53 -0600 received badge  Popular Question (source)
2014-06-04 14:26:46 -0600 asked a question OpenCv: Extract portion of a Mat image

If I have a MatOfPoint that represents a rectangle, how can I extract this MatOfPoint from an image, another Mat. I just want to extract a portion of a image that is represented by the MatOfPoint. I can extract the coordinates from the MatOfPoint , how can I use the 4 coordinates to extract the portion of the Mat?

2014-05-29 09:10:43 -0600 received badge  Editor (source)
2014-05-29 09:06:45 -0600 asked a question Translate c++ code to java

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