gpt4 book ai didi

objective-c - 使用OpenCV和iOS创建封闭区域时将任何图像转换为可着色

转载 作者:行者123 更新时间:2023-12-02 17:50:22 30 4
gpt4 key购买 nike

我正在与iOS和OpenCV一起玩,尝试创建一个游戏,该游戏基本上将任何图像转换为具有多个封闭区域的黑白可着色图像,而我们只需在屏幕上轻按即可为这些区域着色。到目前为止,我取得了部分成功。转换工作正常,但我只能创建带有具体可着色(封闭)区域的图像。

可能吗如果不是,您建议采用哪些替代方法/想法?
我有两个,但我都不喜欢。
-我们在转换后通过绘制来定义区域
-在屏幕上点击仅对x像素进行递归搜索

例:
Dropbox pics

非常感谢任何 build 性的帮助!

我的 View Controller 的相关部分:

- (void)viewDidLoad{
[super viewDidLoad];

imageProcessor = [[Conversion alloc] init];

_image = [UIImage imageNamed:@"lenna"];
imageProcessor.thresholdSlider = self.thresholdSlider;
imageProcessor.thresholdSlider2 = self.thresholdSlider2;

self.imageView.image = [imageProcessor convertImage:_image];
}

- (IBAction)sliderChanged:(id)sender {
self.imageView.image = [imageProcessor convertImage:_image];
}

以及使用OpenCV的Conversion类的相关部分
- (UIImage*)convertImage:(UIImage*)image{

cv::Mat src;
cv::Mat src_gray;

//Convert UIImage to cv::Mat
src = [self cvMatFromUIImage:image];

if (!src.empty()) {
//Convert cv::Mat to grayscale
cv::cvtColor(src, src_gray, CV_RGBA2GRAY);
cv::blur(src_gray, src_gray, cv::Size(3,3));

cv::Mat canny_output;
cv::vector<cv::vector<cv::Point> > contours;
cv::vector<cv::Vec4i> hierarchy;

int thresh = (int)self.thresholdSlider.value;
int thresh2 = (int)self.thresholdSlider2.value;

NSLog(@"%d, %d", thresh, thresh2);

//Detect edges using canny
//http://en.wikipedia.org/wiki/Canny_edge_detector
cv::Canny(src_gray, canny_output, thresh, thresh2, 3, true);

//InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method,
//Point offset = Point())
cv::findContours(canny_output, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0,0));

cv::Scalar blackColor = cv::Scalar::all(0);
cv::Scalar whiteColor = cv::Scalar::all(255);

//create a WHITE Mat with the canny_output size
cv::Mat drawing(canny_output.size(), CV_8UC3, whiteColor);
for (size_t i = 0; i < contours.size(); i++) {
//InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness = 1,
//int lineType = 8, InputArray hierarchy = noArray(), int maxLevel = 2147483647, Point offset = Point())
cv::drawContours(drawing, contours, int(i), blackColor, 1, CV_AA, hierarchy, INT_MAX, cv::Point());
}
return [self UIImageFromCVMat:drawing];

}
return nil;
}

最佳答案

您可以按照以下步骤操作,

  • 查找轮廓。
  • 对于每个轮廓,请按照here的说明检查其是否闭合,然后以适当的颜色绘制轮廓。
  • 关于objective-c - 使用OpenCV和iOS创建封闭区域时将任何图像转换为可着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23189662/

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