python图像分割动态域值_python+opencv阈值分割
37 #獲取像素點的最大值和最小值
38 arr_temp =np.reshape(img_arr,(lens,))39 max_val =max(arr_temp)40 min_val =min(arr_temp)41 #圖像歸一化
42 img_arr = (img_arr-min_val)/(max_val-min_val)43 #繪制圖像并保存
44 #保存圖片時去掉周圍白邊
45 plt.axis('off')46 fig =plt.gcf()47 fig.set_size_inches(7.0/3,7.0/3) #dpi = 300, output = 700*700 pixels
48 plt.gca().xaxis.set_major_locator(plt.NullLocator())49 plt.gca().yaxis.set_major_locator(plt.NullLocator())50 plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace =0)51 plt.margins(0,0)52 plt.imshow(img_arr,cmap=plt.cm.bone)53 fig.savefig(jpgname, format='jpg', transparent=True, dpi=300, pad_inches =0)54
55 #獲取圖像灰度直方圖查看灰度分布
56 img=cv2.imread('D:/1/dicom/test4.jpg')57 plt.hist(img.ravel(),256,[0,256])#ravel函數功能是將多維數組降為一維數組
58 plt.show()59
60 img = cv2.imread('D:/1/dicom/test4.jpg', 0)61 jpgname = 'D:/1/dicom/test58.jpg'
62 #固定閾值
63 ret, th1 = cv2.threshold(img, 165, 255, cv2.THRESH_BINARY)64 images =[img, th1]65 #保存粗分割結果
66 plt.axis('off')67 fig =plt.gcf()68 fig.set_size_inches(7.0/3,7.0/3) #dpi = 300, output = 700*700 pixels
69 plt.gca().xaxis.set_major_locator(plt.NullLocator())70 plt.gca().yaxis.set_major_locator(plt.NullLocator())71 plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace =0)72 plt.margins(0,0)73 plt.imshow(images[1], 'gray')74 fig.savefig(jpgname, format='jpg', transparent=True, dpi=300, pad_inches =0)75 #對粗分割結果中非病灶區域填充
76 #截取圖片中的指定區域或在指定區域添加某一圖片
77 defjie_image(src1):78 src2 = src1[5:125, 280:600]#截取第5行到125行的第280列到600列的區域
79 #cv.imshow("截取", src2)
80 src1[360:480, 280:600] = src2#指定位置填充,大小要一樣才能填充
81 cv2.imshow("合成", src1)82 src = cv2.imread("D:/1/dicom/test58.jpg", 0)83 #cv.imshow("原來", src)
84 jie_image(src)85 cv2.waitKey(0)86 cv2.destroyAllWindows()87 cv2.imwrite('D:/1/dicom/test68.jpg', src)88
89 #填充后得到第二次的粗分割結果,病灶區域存在孔洞,使用孔洞填充方法進行填充
90 '''
91 圖像說明:92 圖像為二值化圖像,255白色為目標物,0黑色為背景93 要填充白色目標物中的黑色空洞94 '''
95 imgPath = 'D:/1/dicom/test68.jpg'
96 SavePath = 'D:/1/dicom/test78.jpg'
97 defFillHole(imgPath,SavePath):98 im_in =cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE);99 #復制 im_in 圖像
100 im_floodfill =im_in.copy()101 #Mask 用于 floodFill,官方要求長寬+2
102 h, w = im_in.shape[:2]103 mask = np.zeros((h+2, w+2), np.uint8)104 #floodFill函數中的seedPoint必須是背景
105 isbreak =False106 for i inrange(im_floodfill.shape[0]):107 for j in range(im_floodfill.shape[1]):108 if(im_floodfill[i][j]==0):109 seedPoint=(i,j)110 isbreak =True111 break
112 if(isbreak):113 break
114 #得到im_floodfill
115 cv2.floodFill(im_floodfill, mask, seedPoint, 255);116 #得到im_floodfill的逆im_floodfill_inv
117 im_floodfill_inv =cv2.bitwise_not(im_floodfill)118 #把im_in、im_floodfill_inv這兩幅圖像結合起來得到前景
119 im_out = im_in |im_floodfill_inv120 #保存結果
121 cv2.imwrite(SavePath, im_out)122 FillHole(imgPath,SavePath
#以上這種填充結果會把輪廓外圍也填充,分割不準確,因此使用了MATLAB中的imfill函數對分割后病灶區域的結果進行孔洞填充,得到的分割結果如下圖所示
clear all; clc; close all;
img = imread('D:\matlab\bin\test68.jpg');
if ndims(img)==3
img = rgb2gray(img);
end
img_bw = im2bw(img);
img_fill = imfill(img_bw, 'holes');
imwrite(img_fill,'D:\matlab\bin\temp34.jpg');
%figure;
%subplot(1,2,1),imshow(img_bw), title('有空洞的圖像');
%subplot(1,2,2),imshow(img_fill), title('孔洞被填充的圖像');
總結
以上是生活随笔為你收集整理的python图像分割动态域值_python+opencv阈值分割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python排序链表_合并K个排序链表
- 下一篇: python 哪些比赛项目_70个超火p