繁体   English   中英

将 OpenCV-Python 转换为 C++/Objective C

[英]Convert OpenCV-Python to C++/Objective C

我从@abid-rahman-k 中发现了这个非常好的代码来检测图像中的矩形: OpenCV C++/Obj-C: Advanced square detection现在代码在 Python 中,它是:

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)>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()

我想将其转换为 Objective C/C++。 这就是我所做的,但没有奏效,我错过了什么?

- (void)processImage2:(cv::Mat&)image;
{
    cv::Mat img,gray,thresh;

    std::vector<std::vector<cv::Point> > 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<cv::Point> hull;
    for (size_t i = 0; i < contours.size(); i++)
    {
        if (cv::contourArea(contours[i])>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);

        }
    }


}

更新:

该程序运行,但在我选择图像后它崩溃了,我收到了这个错误:

Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] <Error>: 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] <Error>: CGContextConcatCTM: invalid context 0x0
Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] <Error>: CGContextDrawImage: invalid context 0x0
Nov 28 10:26:52 Anas-Basalamahs-MacBook-Air.local OpenCV Tutorial[18861] <Error>: 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 < 0) in getMat, file /Users/bloodaxe/Develop/opencv/modules/core/src/matrix.cpp, line 957
terminate called throwing an exception(lldb) 

在这里,这是可行的:

cv::Mat oResult, oToShow;
// roMat is your input image
cv::cvtColor(roMat, oToShow, CV_GRAY2BGR);

cv::threshold(roMat, oResult, 127, 255, 0);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;

cv::findContours( oResult, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE );


std::vector<std::vector<cv::Point> > oHull(contours.size());
for( int i = 0; i != contours.size(); i++)
{
    if( cv::contourArea(contours[i]) > 5000 )
    {
        cv::convexHull(contours[i], oHull[i]);
        cv::approxPolyDP(oHull[i], oHull[i], 0.1*cv::arcLength(oHull[i], true), true);
        if( oHull[i].size() == 4 )
        {
            cv::drawContours(oToShow, oHull, -1, cv::Scalar(0, 255,0), 2, 8);
        }

    }
}

cv::imshow("hull", oToShow);
cv::waitKey(0);

我认为错误是,您实际上得到了一个船体数组 ,而不仅仅是一个船体。

我在您的代码中看不到未读内容,因此,我假设您正在读取此函数之外的图像并将其传递给您。是否在读取后立即尝试执行imshow以确保正确加载了图像。

第一行向我表明这可能是问题所在。

问题出在cv::drawContours(image,hull,0,cv::Scalar(0,255,0),2);

由于某种原因,它不喜欢hull数组...因此,我认为其中之一发生了错误:

cv::convexHull(contours[i],hull);

approxPolyDP(hull, hull, 0.1*arcLength(hull, true), true);

(你不说夏洛克!)

我试图找到一个解决这些链接中的代码的方法:

我希望这份志愿会比他们对我的帮助更大。

另外,很抱歉将其发布为答案...但是我的声誉很低,无法添加评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM