Defining a point and selecting a circle area

I work with a certain type of image. Those, having received a spectrum (aply fft), I get the following image:

enter image description here

So, I want to select one of these “points” (called spectrum orders) as follows:

enter image description here

I mean “draw” a circle around it, select the pixels inside, and then center these pixels (without the “border circle”):

enter image description here

How can I execute it using OpenCV? Is there any function?

+4
source share
2 answers

EDIT: according to the discussion below, you can use a mask to “select” a circle:

# 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, :]

This leaves me with something like:

Masked and cropped image

: .

+5

, , , :

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);

, .

EDIT: , ++, , python?!?

+1

Source: https://habr.com/ru/post/1544931/


All Articles