Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS:Retrieve rectangle shaped image from the background image
    primarykey
    data
    text
    <p>I am working on an implementation where I have a rectangle shaped image in an big background image. I am trying to programmatically retrieve the rectangle shaped image from the big image and retrieve text information from that particular rectangle image. I am trying to use Open-CV third party framework, but couldn't able to retrieve the rectangle image from the big background image. Could someone please guide me, how i can achieve this?</p> <p><strong>UPDATED:</strong></p> <p>I found the <a href="https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/squares.c?rev=27" rel="nofollow">Link</a> to find out the square shapes using OpenCV. Can i get it modified for finding Rectangle shapes? Can someone guide me on this?</p> <p>UPDATED LATEST:</p> <p>I got the code finally, here is it below.</p> <pre><code> - (cv::Mat)cvMatWithImage:(UIImage *)image { CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage); CGFloat cols = image.size.width; CGFloat rows = image.size.height; cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to backing data cols, // Width of bitmap rows, // Height of bitmap 8, // Bits per component cvMat.step[0], // Bytes per row colorSpace, // Colorspace kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault); // Bitmap info flags CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage); CGContextRelease(contextRef); return cvMat; } -(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat { NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()]; CGColorSpaceRef colorSpace; if ( cvMat.elemSize() == 1 ) { colorSpace = CGColorSpaceCreateDeviceGray(); } else { colorSpace = CGColorSpaceCreateDeviceRGB(); } //CFDataRef data; CGDataProviderRef provider = CGDataProviderCreateWithCFData( (CFDataRef) data ); // It SHOULD BE (__bridge CFDataRef)data CGImageRef imageRef = CGImageCreate( cvMat.cols, cvMat.rows, 8, 8 * cvMat.elemSize(), cvMat.step[0], colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault ); UIImage *finalImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease( imageRef ); CGDataProviderRelease( provider ); CGColorSpaceRelease( colorSpace ); return finalImage; } -(void)forOpenCV { imageView = [UIImage imageNamed:@"myimage.jpg"]; if( imageView != nil ) { cv::Mat tempMat = [imageView CVMat]; cv::Mat greyMat = [self cvMatWithImage:imageView]; cv::vector&lt;cv::vector&lt;cv::Point&gt; &gt; squares; cv::Mat img= [self debugSquares: squares: greyMat]; imageView = [self UIImageFromCVMat: img]; self.imageView.image = imageView; } } double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) { double dx1 = pt1.x - pt0.x; double dy1 = pt1.y - pt0.y; double dx2 = pt2.x - pt0.x; double dy2 = pt2.y - pt0.y; return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10); } - (cv::Mat) debugSquares: (std::vector&lt;std::vector&lt;cv::Point&gt; &gt;) squares : (cv::Mat &amp;)image { NSLog(@"%lu",squares.size()); // blur will enhance edge detection //cv::Mat blurred(image); cv::Mat blurred = image.clone(); medianBlur(image, blurred, 9); cv::Mat gray0(image.size(), CV_8U), gray; cv::vector&lt;cv::vector&lt;cv::Point&gt; &gt; contours; // find squares in every color plane of the image for (int c = 0; c &lt; 3; c++) { int ch[] = {c, 0}; mixChannels(&amp;image, 1, &amp;gray0, 1, ch, 1); // try several threshold levels const int threshold_level = 2; for (int l = 0; l &lt; threshold_level; l++) { // Use Canny instead of zero threshold level! // Canny helps to catch squares with gradient shading if (l == 0) { Canny(gray0, gray, 10, 20, 3); // // Dilate helps to remove potential holes between edge segments dilate(gray, gray, cv::Mat(), cv::Point(-1,-1)); } else { gray = gray0 &gt;= (l+1) * 255 / threshold_level; } // Find contours and store them in a list findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); // Test contours cv::vector&lt;cv::Point&gt; approx; for (size_t i = 0; i &lt; contours.size(); i++) { // approximate contour with accuracy proportional // to the contour perimeter approxPolyDP(cv::Mat(contours[i]), approx, arcLength(cv::Mat(contours[i]), true)*0.02, true); // Note: absolute value of an area is used because // area may be positive or negative - in accordance with the // contour orientation if (approx.size() == 4 &amp;&amp; fabs(contourArea(cv::Mat(approx))) &gt; 1000 &amp;&amp; isContourConvex(cv::Mat(approx))) { double maxCosine = 0; for (int j = 2; j &lt; 5; j++) { double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1])); maxCosine = MAX(maxCosine, cosine); } if (maxCosine &lt; 0.3) squares.push_back(approx); } } } } NSLog(@"squares.size(): %lu",squares.size()); for( size_t i = 0; i &lt; squares.size(); i++ ) { cv::Rect rectangle = boundingRect(cv::Mat(squares[i])); NSLog(@"rectangle.x: %d", rectangle.x); NSLog(@"rectangle.y: %d", rectangle.y); if(i==squares.size()-1)////Detecting Rectangle here { const cv::Point* p = &amp;squares[i][0]; int n = (int)squares[i].size(); NSLog(@"%d",n); line(image, cv::Point(507,418), cv::Point(507+1776,418+1372), cv::Scalar(255,0,0),2,8); polylines(image, &amp;p, &amp;n, 1, true, cv::Scalar(255,255,0), 5, CV_AA); int fx1=rectangle.x; NSLog(@"X: %d", fx1); int fy1=rectangle.y; NSLog(@"Y: %d", fy1); int fx2=rectangle.x+rectangle.width; NSLog(@"Width: %d", fx2); int fy2=rectangle.y+rectangle.height; NSLog(@"Height: %d", fy2); line(image, cv::Point(fx1,fy1), cv::Point(fx2,fy2), cv::Scalar(0,0,255),2,8); } } return image; } </code></pre> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload