点检测与圆形区域选择

4 投票
2 回答
5539 浏览
提问于 2025-04-18 10:06

我正在处理一种特定类型的图像。在获取了频谱(应用快速傅里叶变换,简称fft)之后,我得到了如下的图片:

enter image description here

我想以以下方式选择这些“点”(称为频谱的阶数):

enter image description here

我的意思是“画”一个圆圈围绕它,选择圆圈内的像素,然后将这些像素居中(不包括“边框圆圈”):

enter image description here

我该如何使用OpenCV来实现这个呢?有没有现成的函数可以用?

2 个回答

1

我不太确定这是不是你想问的,不过如果你只是想围绕某个点进行居中,可以通过子区域来实现:

cv::Point center(yourCenterX_FromLeft, yourCenterY_fromTop);
int nWidth = yourDesiredWidthAfterCentering;  // or 2* circle radius
int nHeight= yourDesiredHeightAfterCentering; // or 2* circle radius

// specify the subregion: top-left position and width/height
cv::Rect subImage = cv::Rect(center.x-nWidth/2, center.y-nHeight/2, nWidth, nHeight);

// extract the subregion out of the original image. remark that no data is copied but original data is referenced
cv::Mat subImageCentered = originalImage(subImage);

cv::imshow("subimage", subImageCentered);

我没有测试过,但应该没问题。

补充一下:抱歉,这段代码是C++的,不过我觉得在Python中子区域的用法应该也差不多吧?!

6

编辑:根据下面的讨论,要“选择”一个圆形,可以使用一个遮罩:

# Build mask
mask = np.zeros(image_array.shape, dtype=np.uint8)
cv2.circle(mask, max_loc, circle_radius, (255, 255, 255), -1, 8, 0)

# Apply mask (using bitwise & operator)
result_array = image_array & mask

# Crop/center result (assuming max_loc is of the form (x, y))
result_array = result_array[max_loc[1] - circle_radius:max_loc[1] + circle_radius,
                            max_loc[0] - circle_radius:max_loc[0] + circle_radius, :]

这样我得到了类似这样的结果:

经过遮罩和裁剪的图像

另一个编辑:这个链接可能对你找到高峰有帮助。

撰写回答