# OPENCV我写了一个环形ROI提取，但是纯粹是顺序结构（臃肿），求个大佬改成循环结构

Mat circleROI2, circleROI3, circleROI4, circleROI5, circleROI6,circleROI;
Mat circleROI1; //定义圆环ROI
Mat roi1, roi2, roi,out,wzc;
Mat roi3, roi4, roi5, roi6, roi7;
Mat roia, roib, roic, roid, roie;
Mat out1, out2, out3, out4, out5, out6;
Mat wzc1, wzc2, wzc3, wzc4, wzc5, wzc6;
//三个变量都按照source的尺寸初始化为零，
roi1 = Mat::zeros(source.size(), CV_8UC1);
roi2 = Mat::zeros(source.size(), CV_8UC1);
roi3 = Mat::zeros(source.size(), CV_8UC1);
roi4 = Mat::zeros(source.size(), CV_8UC1);
roi5 = Mat::zeros(source.size(), CV_8UC1);
roi6= Mat::zeros(source.size(), CV_8UC1);
roi7= Mat::zeros(source.size(), CV_8UC1);

//在源图像之中画圆，圆的位置和尺寸是手工计算出来的
//circle()函数的最后一个参数-1表示为区域都填充为RGB(255, 255, 255)
int i = 50, b = 50;
circle(roi1, Point(512, 288), 50, CV_RGB(255, 255, 255), -1);
circle(roi2, Point(512, 288), 100, CV_RGB(255, 255, 255), -1);
circle(roi3, Point(512, 288), 150, CV_RGB(255, 255, 255), -1);
circle(roi4, Point(512, 288), 200, CV_RGB(255, 255, 255), -1);
circle(roi5, Point(512, 288), 250, CV_RGB(255, 255, 255), -1);
circle(roi6, Point(512, 288), 300, CV_RGB(255, 255, 255), -1);
circle(roi7, Point(512, 288),350, CV_RGB(255, 255, 255), -1);
//大圆减去小圆就是提出出来的圆环
cv::subtract(roi2, roi1, roi);
cv::subtract(roi3, roi2, roia);
cv::subtract(roi4, roi3, roib);
cv::subtract(roi5, roi4, roic);
cv::subtract(roi6, roi5, roid);
cv::subtract(roi7, roi6, roie);
//把圆环区域的图像复制出来，进行下一步操作，

source.copyTo(circleROI, roi);
source.copyTo(circleROI1, roia);
source.copyTo(circleROI2, roib);
source.copyTo(circleROI3, roic);
source.copyTo(circleROI4, roid);
source.copyTo(circleROI5, roie);
source.copyTo(circleROI6, roi1);

blur(circleROI, out, Size(2, 2));
blur(circleROI1, out1, Size(3, 3));
blur(circleROI2, out2, Size(4, 4));
blur(circleROI3, out3, Size(5, 5));
blur(circleROI4, out4, Size(6, 6));
blur(circleROI5, out5, Size(7, 7));

//显示
imshow("result",wzc5);
waitKey();

• 写回答

#### 2条回答默认 最新

• 泡视界 2020-12-24 17:20
关注
``````Mat source = imread("1.jpg"); //从磁盘之中读取图片

/*Mat circleROI2, circleROI3, circleROI4, circleROI5, circleROI6,circleROI;
Mat circleROI1; //定义圆环ROI
Mat roi1, roi2, roi,out,wzc;
Mat roi3, roi4, roi5, roi6, roi7;
Mat roia, roib, roic, roid, roie;
Mat out1, out2, out3, out4, out5, out6;
Mat wzc1, wzc2, wzc3, wzc4, wzc5, wzc6;
//三个变量都按照source的尺寸初始化为零，
roi1 = Mat::zeros(source.size(), CV_8UC1);
roi2 = Mat::zeros(source.size(), CV_8UC1);
roi3 = Mat::zeros(source.size(), CV_8UC1);
roi4 = Mat::zeros(source.size(), CV_8UC1);
roi5 = Mat::zeros(source.size(), CV_8UC1);
roi6= Mat::zeros(source.size(), CV_8UC1);
roi7= Mat::zeros(source.size(), CV_8UC1);
*/

Mat roi[7];
for (int i = 0; i < 7; i++) {
roi[i] = Mat::zeros(source.size(), CV_8UC1);
}

//在源图像之中画圆，圆的位置和尺寸是手工计算出来的
//circle()函数的最后一个参数-1表示为区域都填充为RGB(255, 255, 255)
/*int i = 50, b = 50;
circle(roi1, Point(512, 288), 50, CV_RGB(255, 255, 255), -1);
circle(roi2, Point(512, 288), 100, CV_RGB(255, 255, 255), -1);
circle(roi3, Point(512, 288), 150, CV_RGB(255, 255, 255), -1);
circle(roi4, Point(512, 288), 200, CV_RGB(255, 255, 255), -1);
circle(roi5, Point(512, 288), 250, CV_RGB(255, 255, 255), -1);
circle(roi6, Point(512, 288), 300, CV_RGB(255, 255, 255), -1);
circle(roi7, Point(512, 288),350, CV_RGB(255, 255, 255), -1);
*/

for (int i = 0; i < 7; i++) {
circle(roi[i], Point(512, 288), b, CV_RGB(255, 255, 255), -1);
b += 50;
}

/*//大圆减去小圆就是提出出来的圆环
cv::subtract(roi2, roi1, roi);
cv::subtract(roi3, roi2, roia);
cv::subtract(roi4, roi3, roib);
cv::subtract(roi5, roi4, roic);
cv::subtract(roi6, roi5, roid);
cv::subtract(roi7, roi6, roie);
//把圆环区域的图像复制出来，进行下一步操作，
*/

Mat roi_sub[6];    //我这里用roi_sub代替你的roi abcde
for (int i = 0; i < 6; i++) {
cv::subtract(roi[i], roi[i], roi_sub[i]);
}

/*source.copyTo(circleROI, roi);
source.copyTo(circleROI1, roia);
source.copyTo(circleROI2, roib);
source.copyTo(circleROI3, roic);
source.copyTo(circleROI4, roid);
source.copyTo(circleROI5, roie);
source.copyTo(circleROI6, roi1);
*/

Mat circleROI[7];
for (int i = 0; i < 7 - 1; i++) {
source.copyTo(circleROI[i], roi_sub[i]);
}
source.copyTo(circleROI[6], roi[0]);  //这个你代码是这么赋值的

/*blur(circleROI, out, Size(2, 2));
blur(circleROI1, out1, Size(3, 3));
blur(circleROI2, out2, Size(4, 4));
blur(circleROI3, out3, Size(5, 5));
blur(circleROI4, out4, Size(6, 6));
blur(circleROI5, out5, Size(7, 7));
*/

Mat out[6];
for (int i = 0; i < 6; i++) {
blur(circleROI[i], out[i], Size(i + 2, i + 2));
}

*/

Mat wzc[6];
for (int i = 0; i < 6 - 1; i++) {
add(wzc[i], out[i + 1], wzc[i + 1]);
}
//显示
/*imshow("result",wzc5);*/

imshow("result",wzc[5]);

waitKey();``````

我猜你是这个意思吧

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 CCF-CSP 2023 第三题 解压缩(50％)
• ¥30 comfyui openpose报错
• ¥20 Wpf Datarid单元格闪烁效果的实现
• ¥15 图像分割、图像边缘提取
• ¥15 sqlserver执行存储过程报错
• ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
• ¥15 浮窗和全屏应用同时存在，全屏应用输入法无法弹出
• ¥100 matlab2009 32位一直初始化
• ¥15 Expected type 'str | PathLike[str]…… bytes' instead
• ¥15 三极管电路求解，已知电阻电压和三级关放大倍数