gpt4 book ai didi

c++ - 使用opencv进行图像边缘平滑

转载 作者:技术小花猫 更新时间:2023-10-29 10:44:02 28 4
gpt4 key购买 nike

我正在尝试使用 opencv 框架平滑输出图像边缘,我正在尝试执行以下步骤。从这里采取的步骤 https://stackoverflow.com/a/17175381/790842

int lowThreshold = 10.0;
int ratio = 3;
int kernel_size = 3;

Mat src_gray,detected_edges,dst,blurred;

/// Convert the image to grayscale
cvtColor( result, src_gray, CV_BGR2GRAY );

/// Reduce noise with a kernel 3x3
cv::blur( src_gray, detected_edges, cv::Size(5,5) );

/// Canny detector
cv::Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );

//Works fine upto here I am getting perfect edge mask

cv::dilate(detected_edges, blurred, result);

//I get Assertion failed (src.channels() == 1 && func != 0) in countNonZero ERROR while doing dilate

result.copyTo(blurred, blurred);

cv::blur(blurred, blurred, cv::Size(3.0,3.0));

blurred.copyTo(result, detected_edges);

UIImage *image = [UIImageCVMatConverter UIImageFromCVMat:result];

我需要帮助,无论我走的路是否正确,或者我错过了什么?

感谢您的任何建议和帮助。

更新:

我从 grabcut 算法中得到了一张如下图所示的图像,现在我想对图像应用边缘平滑处理,如您所见,图像并不平滑。 enter image description here

最佳答案

你想得到这样的东西吗?

enter image description here

如果是,那么代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
cv::namedWindow("result");
Mat img=imread("TestImg.png");
Mat whole_image=imread("D:\\ImagesForTest\\lena.jpg");
whole_image.convertTo(whole_image,CV_32FC3,1.0/255.0);
cv::resize(whole_image,whole_image,img.size());
img.convertTo(img,CV_32FC3,1.0/255.0);

Mat bg=Mat(img.size(),CV_32FC3);
bg=Scalar(1.0,1.0,1.0);

// Prepare mask
Mat mask;
Mat img_gray;
cv::cvtColor(img,img_gray,cv::COLOR_BGR2GRAY);
img_gray.convertTo(mask,CV_32FC1);
threshold(1.0-mask,mask,0.9,1.0,cv::THRESH_BINARY_INV);

cv::GaussianBlur(mask,mask,Size(21,21),11.0);
imshow("result",mask);
cv::waitKey(0);


// Reget the image fragment with smoothed mask
Mat res;

vector<Mat> ch_img(3);
vector<Mat> ch_bg(3);
cv::split(whole_image,ch_img);
cv::split(bg,ch_bg);
ch_img[0]=ch_img[0].mul(mask)+ch_bg[0].mul(1.0-mask);
ch_img[1]=ch_img[1].mul(mask)+ch_bg[1].mul(1.0-mask);
ch_img[2]=ch_img[2].mul(mask)+ch_bg[2].mul(1.0-mask);
cv::merge(ch_img,res);
cv::merge(ch_bg,bg);

imshow("result",res);
cv::waitKey(0);
cv::destroyAllWindows();
}

而且我认为您也会对这个链接感兴趣:Poisson Blending

关于c++ - 使用opencv进行图像边缘平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com