Visualization of segmentation results: How to display the label mask outline on the original image

After training the model to obtain the segmentation structure of the optic disc and optic cup, check the example image in the paper. The segmentation results are displayed as:
Insert image description here
The following are some problems encountered during the visualization process

Question 1: How to visualize npy files

cv2.imshow("ima", ima) # 需要两个参数 记住图片名称不可少
cv2.waitKey() # 等待时间 不写就是手动 0就是1s后会自动关闭

Notice! ! !
The reason for the failure is that when cv2 is displayed, the image must be (h, w, c). The number of channels is at the end, otherwise an error will be reported. The
conversion dimension ima = image.transpose(1, 2, 0).
The color of the image displayed here is also very strange.
After consulting the information, I found :
If you want to use np.load to read pictures and use cv2.imshow to output normal photos, you can just adjust the order of color channels from BGR to RGB during output.
The interface of opencv uses BGR, while matplotlib.pyplot is in RGB mode, so just swap the number of channels.

import cv2
 
image=np.load(image1)
ima = image.transpose(1, 2, 0) # 转化为(h,w,c)
ima_re = ima[:,:,::-1] # 转换成正常的颜色通道
cv2.imshow("ima", ima_re) # 需要两个参数 记住图片名称不可少
cv2.waitKey() # 等待时间 不写就是手动 0就是1s后会自动关闭

![[Pasted image 20221115145419.png|200]]![[Pasted image 20221115150023.png|200]]

Question 2: How to save this jpg file
? This Image is very normal. Note that .astype(np.uint8) sometimes causes strange errors because of it.

from PIL import Image
file_name = file.split(".")[0] + "_" + file.split(".")[1] + '.jpg'
save_full_path = os.path.join(save_path, file_name)
img = Image.fromarray(res.astype(np.uint8), 'RGB')
img.save(save_full_path)

Question 3: How to display the outline of the segmentation result mask on the original image.
After searching for csdn for a long time, I finally successfully obtained my segmentation result image.

Note: Before using the following method, my mask only had two values ​​0-1.
1. Convert the mask to a black background. White is the segmentation mask.
Use the image binarization function cv2.threshold function
to binarize the image, which is to convert the image on The gray value of the pixel is set to 0 or 255, which means that the entire image presents an obvious visual effect of only black and white (gray value 0: black, gray value 255: white). Reason: In the
image In addition to the target object and background area, there is also noise, which will cause problems for us to recognize the image, so we need to directly extract the target image from the multi-valued digital image through the image binarization function, that is to say, set a The threshold T is used to divide the pixel group of the image into two.

cv2.thresholdcv2.threshold(src, thresh, maxval, type) → ret, thresh
in the function:
src: represents the original image
thresh: represents the threshold
maxval: represents the maximum value
type: represents the algorithm used when dividing, Binarization method, the common value is 0 (cv2.THRESH_BINARY)
ps: greater than the threshold is the maximum value, less than the threshold is 0. The threshold here needs to be chosen by ourselves (my mask is 0-1, so I chose 0.5)

The first ret (the obtained threshold value (here is 0.0)), and the second one is the thresholded image thresh.

A problem occurred: The program did not respond at all. It seemed to be running but seemed to be stuck.

#错误写法:
ret, thresh = cv2.threshold(mask_dic, 0.5, 255, 0)
#正确写法
ret, thresh = cv2.threshold(mask_dic.astype(np.uint8), 0.5, 255, 0)

2. Obtain the outline of the binarized mask

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset ]]])
function parameters:
image: The parameter is the image to find the contour;
mode: The parameter indicates the retrieval mode of the contour, there are four types (introduced below They are all new cv2 interfaces):
cv2.RETR_EXTERNAL: Indicates that only the outer contour is detected.
cv2.RETR_LIST: The detected contours do not establish a hierarchical relationship.
cv2.RETR_CCOMP: Creates two levels of contours. The upper layer is the outer boundary, and the inner one is The layer is the boundary information of the inner hole. If there is another connected object in the inner hole, the boundary of this object is also on the top layer.
cv2.RETR_TREE: Create an outline of a hierarchical tree structure.
method: approximate method of contour:
cv2.CHAIN_APPROX_NONE: stores all contour points, and the pixel position difference between two adjacent points does not exceed 1

Return value:
The cv2.findContours() function returns two values, one is the contour itself, and the other is the attributes corresponding to each contour.
Contour return value: Returns a list. Each element in the list is a contour in the image, represented by ndarray in numpy.
hierarchy return value: Returns an ndarray with the same number of elements as the number of outlines.

contours, im = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 第一个参数是轮廓

3. Draw the contour you just found on the original image
cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None) The first
parameter specifies where Which image to draw the contour on; the image must have three channels to display the contour.
The second parameter is the contour itself, which is a list in Python;
the third parameter specifies which contour in the contour list to draw. If it is -1, then draw it. All outlines in it. The following parameters are very simple. Thickness indicates the width of the outline. If it is -1 (cv2.FILLED), it is fill mode.

An error occurred:
TypeError: Expected Ptrcv::UMat for argument 'image'
The reason is:
There is a conflict between the image data, and the .copy function needs to be called in some places. Add
.copy to the original image and save it later as above.

If there are multiple masks, just repeat the same process and continue to draw the outline on the first mask outline.

ret, thresh = cv2.threshold(mask_dic.astype(np.uint8), 0.5, 255, 0)
contours, im = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 第一个参数是轮廓
res1=cv2.drawContours(ima.copy(), contours=contours, contourIdx=-1, color=(64,224, 208), thickness=1)

Get the following image:
Insert image description here

Guess you like

Origin blog.csdn.net/ZLInbao/article/details/127867774