Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert OpenCV-Python to C++/Objective C
    primarykey
    data
    text
    <p>I found this very nice code from @abid-rahman-k to detect a rectangle from image: <a href="https://stackoverflow.com/questions/10533233/opencv-c-obj-c-advanced-square-detection">OpenCV C++/Obj-C: Advanced square detection</a> Now the code is in Python and here it is:</p> <pre><code>import cv2 import numpy as np img = cv2.imread('sof.jpg') img = cv2.resize(img,(500,500)) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,0) contours,hier = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: if cv2.contourArea(cnt)&gt;5000: # remove small areas like noise etc hull = cv2.convexHull(cnt) # find the convex hull of contour hull = cv2.approxPolyDP(hull,0.1*cv2.arcLength(hull,True),True) if len(hull)==4: cv2.drawContours(img,[hull],0,(0,255,0),2) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows() </code></pre> <p>I would like to convert it into Objective C/ C++. This is what I did, but did not work, what did I miss?</p> <pre><code>- (void)processImage2:(cv::Mat&amp;)image; { cv::Mat img,gray,thresh; std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours; // cv::resize(image.clone(), image, cv::Size(500,500) ); cvtColor(image, gray, cv::COLOR_BGR2GRAY ); cv::threshold(gray, thresh, 127, 255, 0); findContours(thresh, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); std::vector&lt;cv::Point&gt; hull; for (size_t i = 0; i &lt; contours.size(); i++) { if (cv::contourArea(contours[i])&gt;5000){ cv::convexHull(contours[i],hull); approxPolyDP(hull, hull, 0.1*arcLength(hull, true), true); if (hull.size() ==4) cv::drawContours(image,hull,0,cv::Scalar(0,255,0),2); } } } </code></pre> <p>Update:</p> <p>The program runs but after I select the image it crashes and I get this error:</p> <pre><code>Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] &lt;Error&gt;: CGBitmapContextCreate: unsupported parameter combination: 0 integer bits/component; 0 bits/pixel; 0-component color space; kCGImageAlphaNone; 0 bytes/row. Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] &lt;Error&gt;: CGContextConcatCTM: invalid context 0x0 Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] &lt;Error&gt;: CGContextSetInterpolationQuality: invalid context 0x0 Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] &lt;Error&gt;: CGContextDrawImage: invalid context 0x0 Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] &lt;Error&gt;: CGBitmapContextCreateImage: invalid context 0x0 2012-11-28 10:26:52.963 OpenCV Tutorial[18861:c07] resized image size: NSSize: {0, 0} OpenCV Error: Assertion failed (i &lt; 0) in getMat, file /Users/bloodaxe/Develop/opencv/modules/core/src/matrix.cpp, line 957 terminate called throwing an exception(lldb) </code></pre>
    singulars
    1. This table or related slice is empty.
    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