You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提取图像中正方形区域并去除相似颜色的突出部分?

解决图像右上角正方形区域提取的问题

针对你要提取图像右上角正方形区域,但因颜色相似混入其他突出部分、且正方形边缘存在轻微变形的问题,我来帮你优化现有代码并给出针对性解决方案:

现有代码的问题分析

你当前的全局图像增强+形态学处理思路存在几个容易导致问题的点:

  • 中值滤波核33过大,会过度模糊正方形的边缘细节,反而加剧变形问题
  • 形态学开运算的99x99核尺寸太大,可能误侵蚀正方形边缘,同时把其他区域的相似颜色区域也合并进来
  • 没有聚焦到右上角的目标区域,全局处理容易将图像其他位置的相似色块误识别

改进后的实现方案

我们通过局部ROI聚焦+精准轮廓筛选+边缘变形校正的思路来解决问题,具体步骤如下:

步骤1:先截取右上角感兴趣区域(ROI)

先缩小处理范围,只针对右上角区域操作,避免其他区域干扰:

cv::Mat rectified_CCD_img = cv::imread("img.png");
// 根据你的图像实际尺寸调整ROI坐标,这里假设右上角占图像右半部分
int roi_x = rectified_CCD_img.cols * 0.5;
int roi_y = 0;
int roi_width = rectified_CCD_img.cols * 0.5;
int roi_height = rectified_CCD_img.rows * 0.5;
cv::Mat roi = rectified_CCD_img(cv::Rect(roi_x, roi_y, roi_width, roi_height));

步骤2:优化预处理,保留边缘同时抑制噪声

调整滤波、增强参数,避免过度模糊边缘细节:

cv::Mat img_gray, blurred_img, clahe_img, threshold_img;
cv::cvtColor(roi, img_gray, cv::COLOR_BGR2GRAY);
// 用较小的中值滤波核,平衡去噪与边缘保留
cv::medianBlur(img_gray, blurred_img, 7);
// 降低CLAHE的clipLimit,避免过度增强噪声
cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE(2.0, cv::Size(8, 8));
clahe->apply(blurred_img, clahe_img);
// 用自适应阈值替代OTSU,更适配局部对比度差异
cv::adaptiveThreshold(clahe_img, threshold_img, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY_INV, 11, 2);

步骤3:轮廓检测与正方形精准筛选

通过轮廓的几何特征,排除其他相似颜色区域,锁定目标正方形:

std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(threshold_img, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

cv::Mat rectified_square;
for (size_t i = 0; i < contours.size(); i++) {
    // 计算轮廓的近似多边形
    double perimeter = cv::arcLength(contours[i], true);
    std::vector<cv::Point> approx;
    cv::approxPolyDP(contours[i], approx, 0.04 * perimeter, true);
    
    // 筛选正方形:4个顶点、凸形、长宽比接近1、面积符合预期
    if (approx.size() == 4 && cv::isContourConvex(approx)) {
        double width = cv::norm(approx[0] - approx[1]);
        double height = cv::norm(approx[1] - approx[2]);
        double aspect_ratio = std::max(width, height) / std::min(width, height);
        double area = cv::contourArea(contours[i]);
        
        if (aspect_ratio < 1.1 && area > 1000 && area < roi_width * roi_height * 0.8) {
            // 透视校正解决边缘变形问题,得到规整正方形
            cv::Point2f src_pts[4] = {approx[0], approx[1], approx[2], approx[3]};
            cv::Point2f dst_pts[4] = {cv::Point2f(0,0), cv::Point2f(roi_width,0), cv::Point2f(roi_width,roi_height), cv::Point2f(0,roi_height)};
            cv::Mat warp_mat = cv::getPerspectiveTransform(src_pts, dst_pts);
            cv::warpPerspective(roi, rectified_square, warp_mat, cv::Size(roi_width, roi_height));
            break;
        }
    }
}
// 保存最终提取并校正的正方形
cv::imwrite("target_square.png", rectified_square);

关键优化点说明

  • 局部ROI处理:只聚焦目标区域,从根源减少相似颜色区域的干扰
  • 参数精细化:调整滤波、增强参数,平衡去噪与边缘保留
  • 轮廓特征筛选:通过多边形顶点数、长宽比、面积等条件,精准锁定正方形
  • 透视校正:解决边缘轻微变形问题,输出规整的正方形区域

内容的提问来源于stack exchange,提问作者Soonmyun Jang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 19:17:45