我已经在我的iPhone应用程序中集成了OpenCV,并设法拍摄了一张照片,然后使用以下代码对其进行处理。现在,我想要实现findcontour函数,以便获得轮廓和层次结构,就像我在Python中所做的那样(代码如下)。这段代码有人能帮上忙吗?
// OpenCVWrapper.mm
@implementation OpenCVWrapper : NSObject
+(UIImage *)ConvertImage:(UIImage *)image {
cv::Mat mat;
UIImageToMat(image, mat);
cv::Mat median;
cv::medianBlur(mat, median, 3);
cv::Mat gray;
cv::cvtColor(median, gray, CV_RGB2GRAY); //or use COLOR_BGR2GRAY
cv::Mat denoise;
cv::fastNlMeansDenoising(gray, denoise, 30.0, 7, 21);
//here I want to enter the converted Python code from below in order to get the largest contour of the image
//cv::findContours(denoise, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
UIImage *binImg = MatToUIImage(contour);
return binImg;
}
@endPython代码是:
image, contours, hierarchy = cv2.findContours(denoise,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours = [(c, cv2.contourArea(c)) for c in contours]
# sort contours list by area, biggest first
contours = sorted(contours, key=lambda c: c[1], reverse=True)
cnt = contours[0]
img = cv2.drawContours(frame, cnt, 0, (0,255,0), 2)我实际上不确定是否可以处理UIImage,然后只返回轮廓(透明背景),以便可以将其叠加到视频流中。以下是使用captureOutput的ViewController中的代码:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,
from connection: AVCaptureConnection!) {
guard let uiImage = imageFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
DispatchQueue.main.async(execute: {
self.imageView.image = OpenCVWrapper.convert(uiImage) ///I am really not sure whether this is the right code to show the uiImage on top of the live stream. Have not really gotten to that issue as yet, but any assistance is welcome发布于 2018-08-11 18:55:16
实际上,我想通了。以下是可行的解决方案:
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = canny.clone();
cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
int largest_area=0;
int largest_contour_index=0;
// iterate through each contour.
for( int i = 0; i< contours.size(); i++ )
{
// Find the area of contour
double a=contourArea( contours[i],false);
if(a>largest_area){
largest_area=a;std::cout<<i<<" area "<<a<<std::endl;
// Store the index of largest contour
largest_contour_index=i;
}
}
cv::Mat contourImage(canny.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar color;
color = cv::Scalar(255, 0, 0);
cv::drawContours(contourImage, contours, largest_contour_index, color);https://stackoverflow.com/questions/51798006
复制相似问题