Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取

        Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取。使用的方法简单朴素,调用OpenCV的同时,自己写了个图像最大颜色区块的相似判别,最后输出该图像。

代码如下:

import cv2
import numpy as np

color_mofang = []

image_1 = cv2.imread("G:\\videos\\mofang.jpeg")

#image_2 = image_1

a,b,c = image_1.shape

print(a,b,c)
s = ""
for i in range(a):
    for j in range(b):
        s = ""
        if(image_1[i,j,0]<=225 and image_1[i,j,1]<=225 and image_1[i,j,2]<=225):
         if(image_1[i,j,0]>=45 and image_1[i,j,1]>=45 and image_1[i,j,2]>=45):
        #    color_mofang.append(str(image_1[i,j,0])+str(image_1[i,j,1])+str(image_1[i,j,2]))
                if(abs(image_1[i,j,0]-image_1[i,j,1])>=45 and abs(image_1[i,j,2]-image_1[i,j,1])>=45 and abs(image_1[i,j,2]-image_1[i,j,0])>=45):
                    for k in range(3):
                     image_1[i,j,k] = int(image_1[i,j,k]/2)*2
                     if(image_1[i,j,k]<10):
                        s = s + "00" + str(image_1[i,j,k])
                     elif (image_1[i,j,k]<100): 
                        s = s + "0" + str(image_1[i,j,k])
                     else:
                        s = s + str(image_1[i,j,k])    
                    color_mofang.append(s)

color_mofang_2 = []

for x in color_mofang:
    if(x not in color_mofang_2):
        color_mofang_2.append(x)

d = []
for x in color_mofang_2:
    d.append(color_mofang.count(x))

max_color = max(d)

i = d.index(max_color)

#print(d)

print(color_mofang_2[i])

aa = int(color_mofang_2[i][0:2])
bb = int(color_mofang_2[i][3:5])
cc = int(color_mofang_2[i][6:8])

for i in range(a):
    for j in range(b):

      if(not(image_1[i,j,0]<=225 and image_1[i,j,1]<=225 and image_1[i,j,2]<=225)):
           for k in range(3):
            image_1[i,j,k]=255 
            

      if(not(image_1[i,j,0]>=25 and image_1[i,j,1]>=25 and image_1[i,j,2]>=25)):
           for k in range(3):
            image_1[i,j,k]=255         

     #flag2 = image_1[i,j,0]>=105 or image_1[i,j,1]>=105 or image_1[i,j,2]>=105 
     #flag2 = True  
     #if(not((abs(image_1[i,j,0]-image_1[i,j,1])>=45) and abs(image_1[i,j,2]-image_1[i,j,1])>=45 and abs(image_1[i,j,2]-image_1[i,j,0])>=45) and flag2): 
      #flag = image_1[i,j,0]>=105 and image_1[i,j,1]>=105 and image_1[i,j,2]>=105
      flag = True  
      if((abs(image_1[i,j,0]-aa)<6 and abs(image_1[i,j,1]-bb)<6 and abs(image_1[i,j,2]-cc)<6) or not flag):
    #    if(not(image_1[i,j,0]==cc and image_1[i,j,1]==bb and image_1[i,j,2]==aa) or (not flag)):
    #   if(not(image_1[i,j,0]==aa and image_1[i,j,1]==bb and image_1[i,j,2]==cc)):        
          for k in range(3):
            image_1[i,j,k]=255            

cv2.imshow("final_image",image_1)
cv2.waitKey(0)
cv2.imwrite("G:\\videos\\mofang2.jpeg",image_1)

filename="G:\\videos\\mofang2.jpeg"
img = cv2.imread(filename,0)
print(np.shape(img))
kernel = np.ones((3,3),np.uint8)
dilate = cv2.dilate(img,kernel,iterations = 1)
# 显示图片
# ## 效果展示
# cv2.imshow('origin', img)
 
 
cv2.imwrite('G:\\videos\\lishuwang_dilate.jpg',dilate)
# erosion = cv2.erode(img,kernel,iterations = 1)
# cv2.imwrite('lishuwang_erosion.jpg',erosion)
 
canny1=cv2.Canny(dilate,100,200)
cv2.imwrite('G:\\videos\\lishuwang_canny.jpg',canny1)
 
# kernel2 = np.ones((2,1),np.uint8)
# erosion = cv2.erode(canny,kernel2,iterations = 1)
# cv2.imwrite('lishuwang_erosion.jpg',erosion)
 
_, labels, stats, centroids = cv2.connectedComponentsWithStats(canny1)
print(centroids)
print("stats",stats)
i=0
for istat in stats:
    if istat[4]<2:
        #print(i)
        print(istat[0:2])
        if istat[3]>istat[4]:
            r=istat[3]
        else:r=istat[4]
        cv2.rectangle(canny1,tuple(istat[0:2]),tuple(istat[0:2]+istat[2:4]) , 0,thickness=-1)  # 26
    i=i+1
 
 
cv2.imwrite('G:\\videos\\lishuwang_canny1.jpg',canny1)

cv2.imshow("final_image",canny1)
cv2.waitKey(0)
cv2.imwrite("G:\\videos\\mofang3.jpeg",canny1)

效果如下:

Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取_第1张图片

Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取_第2张图片 Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取_第3张图片

 

 颜色区块提取和轮廓分割效果如下:

Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取_第4张图片

Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取_第5张图片 

Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取_第6张图片 

Python3使用OpenCV实现图像最大颜色块的判别和图像边缘分割与提取_第7张图片 

 

你可能感兴趣的:(Python,编程和应用实现,opencv,python,计算机视觉)