簡體   English   中英

使用python中的opencv識別圖像中的顏色

[英]Color identification in an image using opencv in python

![在此處輸入圖片描述][1]

能夠識別並獲得藍色,但無法識別紅色顏色范圍,無法修復紅色/紫色條紋的顏色范圍。我使用了輪廓並為紅色、綠色、藍色創建了范圍。條紋顏色范圍不正確我試過設置紅色/紫色的最大范圍

import numpy as np
import cv2 
img = cv2.imread(r'/home/pavithra/Downloads/pic.jpeg')

hsvFrame = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 

# Set range for red color and  
red_lower = np.array([136, 86, 86], np.uint8) 
red_upper = np.array([239, 12, 50], np.uint8) 
red_mask = cv2.inRange(hsvFrame, red_lower, red_upper) 


green_lower = np.array([25, 52, 72], np.uint8) 
green_upper = np.array([102, 255, 255], np.uint8) 
green_mask = cv2.inRange(hsvFrame, green_lower, green_upper) 

blue_lower = np.array([94, 80, 2], np.uint8) 
blue_upper = np.array([120, 255, 255], np.uint8) 
blue_mask = cv2.inRange(hsvFrame, blue_lower, blue_upper) 
    
kernal = np.ones((5, 5), "uint8") 
red_mask = cv2.dilate(red_mask, kernal) 
res_red = cv2.bitwise_and(img, img,  
                            mask = red_mask) 
    
# For green color 
green_mask = cv2.dilate(green_mask, kernal) 
res_green = cv2.bitwise_and(img, img, 
                            mask = green_mask) 
    
# For blue color 
blue_mask = cv2.dilate(blue_mask, kernal) 
res_blue = cv2.bitwise_and(img, img, 
                            mask = blue_mask) 

# Creating contour to track red color 
contours, hierarchy = cv2.findContours(red_mask, 
                                        cv2.RETR_TREE, 
                                        cv2.CHAIN_APPROX_SIMPLE) 
      
for pic, contour in enumerate(contours): 
    area = cv2.contourArea(contour) 
    if(area > 300): 
        x, y, w, h = cv2.boundingRect(contour) 
        img = cv2.rectangle(img, (x, y),  
                                    (x + w, y + h),  
                                    (0, 255, 0), 2) 
            
        cv2.putText(img, "Red", (x, y), 
                    cv2.FONT_HERSHEY_SIMPLEX, 1.0, 
                    (0, 255, 0),2)     


contours, hierarchy = cv2.findContours(green_mask, 
                                        cv2.RETR_TREE, 
                                        cv2.CHAIN_APPROX_SIMPLE) 
    
for pic, contour in enumerate(contours): 
    area = cv2.contourArea(contour) 
    if(area > 300): 
        x, y, w, h = cv2.boundingRect(contour) 
        img = cv2.rectangle(img, (x, y),  
                                    (x + w, y + h), 
                                    (0, 255, 0), 2) 
            
        cv2.putText(img, "Green", (x, y), 
                    cv2.FONT_HERSHEY_SIMPLEX,  
                    1.0, (0, 255, 0),2) 


contours, hierarchy = cv2.findContours(blue_mask, 
                                        cv2.RETR_TREE, 
                                        cv2.CHAIN_APPROX_SIMPLE) 
for pic, contour in enumerate(contours): 
    area = cv2.contourArea(contour) 
    if(area > 300): 
        x, y, w, h = cv2.boundingRect(contour) 
        img = cv2.rectangle(img, (x, y), 
                                    (x + w, y + h), 
                                    (255, 0, 0), 2) 
            
        cv2.putText(img, "Blue", (x, y), 
                    cv2.FONT_HERSHEY_SIMPLEX, 
                    1.0, (255, 0, 0),2) 
            
resize = cv2.resize(img, (800, 480))
cv2.imshow("All clg", resize) 
cv2.waitKey(0)

我將圖像分成兩半只是為了避免不必要的降噪。 然后旋轉圖像使文本水平。 將圖像二值化,得到三個大的藍色 covid19 標簽。 根據他們的 y 坐標(從上到下)對它們進行排序。 執行一些操作以獲得感興趣區域(ROI,我命名為標簽的兩條條紋)。 將每個label二值化,得到彩色條紋的兩個輪廓。 調整了標簽的伽瑪,使 colors 變得更暗一些。 獲得每個條紋的紅色分量。 根據它們的 x 坐標(從左到右)對它們進行排序。 為具有最高紅色值的條紋分配紅色,其他為紫色。

代碼:

    def adjust_gamma(image, gamma=1.0):
        # build a lookup table mapping the pixel values [0, 255] to
        # their adjusted gamma values
        invGamma = 1.0 / gamma
        table = np.array([((i / 255.0) ** invGamma) * 255
            for i in np.arange(0, 256)]).astype("uint8")
        # apply gamma correction using the lookup table
        return cv2.LUT(image, table)

    def rotate(image: np.ndarray,angle, background_color): 
        old_width, old_height = image.shape[:2]
        angle_radian = math.radians(angle)
        width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width)
        height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height)
        image_center = tuple(np.array(image.shape[1::-1]) / 2)
        rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)  
        rot_mat[1, 2] += (width - old_width) / 2
        rot_mat[0, 2] += (height - old_height) / 2
        return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background_color)

    img = cv2.imread("covid.jpg")
    rows,cols = img.shape[:2]
    img = img[:,:cols//2]
    rot = rotate(img,90,(255,255,255))
    gray = cv2.cvtColor(rot,cv2.COLOR_BGR2GRAY)
    otsu = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
    opening = cv2.morphologyEx(otsu,cv2.MORPH_OPEN,np.ones((3,3),np.uint8),iterations=3)
    dilate = cv2.dilate(opening,np.ones((1,5),np.uint8),iterations=5)
    dilate2 = cv2.dilate(dilate,np.ones((3,3),np.uint8),iterations=3)
    contours,_ = cv2.findContours(dilate2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    lables = [] # the three covid19 
    y_coords = [] # to sort the lables from top to bottom (the one with single color comes 1st)
    for cnt in contours:
        x,y,w,h = cv2.boundingRect(cnt)
        x = x + 10
        w = w - 20
        y_coords.append(y)
        lables.append(rot[y+h//3:y+h-h//3,x+w//2:x+int(w*0.70)])
    y_coords,lables = zip(*sorted(zip(y_coords,lables))) # sorting the lables from top to bottom
    y_coords = list(y_coords)
    lables = list(lables)

    otsus = []
    grays = []
    for image in lables:
        gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        grays.append(gray)
        mean = np.mean(gray)
        median = np.median(gray)
        otsus.append(cv2.threshold(gray,median-10,255,cv2.THRESH_BINARY)[1])
    s = [] # just a list to store all the red and purple stripes
    for i in range(len(otsus)):
        otsus[i] = ~otsus[i]
        rows,cols = otsus[i].shape[:2]
        M = np.float32([[1,0,5],[0,1,5]])
        lables[i] = cv2.warpAffine(lables[i],M,(cols+5,rows+10)) # giving some padding
        
        # adjusting the gamma to make the colors more dark
        lables[i] = adjust_gamma(lables[i],gamma=0.40)
        
        otsus[i] = cv2.warpAffine(otsus[i],M,(cols+5,rows+10)) # same padding
        strips,_ = cv2.findContours(otsus[i],cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        x_coords = [] # to sort the red and purple stripes from left to right
        color = ["red","red"] # setting inital colors both to red 
        red_components = []
        for j in range(len(strips)):
            x,y,w,h = cv2.boundingRect(strips[j])
            x_coords.append(x)
            # strip = lables[i][y:y+h,x:x+w,:]
            # strip = lables[i][y+h//4:y+h-h//4,x+w//4:x+w-w//4,:]
            strip = lables[i][y:y+h,x+w//5:x+w-w//5,:]
            s.append(strip)
            try:
                strip_color_b = int(np.mean(strip[0]))
                strip_color_g = int(np.mean(strip[1]))
                strip_color_r = int(np.mean(strip[2]))
                # strip_color = (strip_color_b,strip_color_g,strip_color_r)
                red = strip_color_r
                red_components.append(red)
            except IndexError:
                print("Lable number ",i,"has only a single color.")

        x_coords,red_components = zip(*sorted(zip(x_coords,red_components))) # simultaneously sorting the red and purple stripes from left to right 
        
        try:
            if red_components[0] < red_components[1]:
                color[0] = "purple"
            else:
                color[1] = "purple"
            # print("red components = ",red_components)
            print("Lable = ",i,"strip = ",0,"color = ",color[0])
            print("Lable = ",i,"strip = ",1,"color = ",color[1]) # LEFT to RIGHT
        except IndexError:
            print("Lable number ",i,"is excluded. Continuing with further labels.")

    print("TOTAL NUMBER OF LABLES = ",len(lables))   
        
    # reading the text
    custom_oem_psm_config = r'--oem 3 --psm 3'
    print(pytesseract.image_to_string(otsu,config=custom_oem_psm_config))

    cv2.imshow("Lable 0",lables[0])
    cv2.imshow("Lable 1",lables[1])
    cv2.imshow("Lable 2",lables[2])
    cv2.imshow("rotated",rot)
    cv2.waitKey(0)

OUTPUT:

Lable number  0 has only a single color.
Lable number  0 is excluded. Continuing with further labels.
Lable =  1 strip =  0 color =  purple
Lable =  1 strip =  1 color =  red
Lable =  2 strip =  0 color =  purple
Lable =  2 strip =  1 color =  red
TOTAL NUMBER OF LABLES =  3
COVID-19Aq
COVID-19Ag

如果我錯了,請糾正我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM