Detection of rust with OpenCV (Python) Part 2

asked 2017-11-21 12:10:10 -0600

Camerutttt gravatar image

updated 2017-11-21 12:20:40 -0600

This is a follow up to the previous post: Detection of rust with OpenCV (Python)

Original Image: Rust Image

After reading through the comments on the previous post, we tried working in HSV instead of BGR. However, there wasn't too much of a difference.

Result

This is our current code with reference from here: Colour Detection HSV

import cv2 
import numpy as np

img = cv2.imread('/home/brendanloe/img.jpeg', 1)

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

boundaries1 = [ ([169, 100 , 100], [189, 255, 255]) ]
boundaries2 = [ ([3, 100, 100], [17, 255, 255]) ]
boundaries3 = [ ([2, 100, 100], [22, 255, 255]) ]
boundaries4 = [ ([6, 100, 100], [26, 255, 255]) ]

for (lower1, upper1) in boundaries1:
    lower1 = np.array(lower1, dtype = "uint8")
    upper1 = np.array(upper1, dtype = "uint8")
    mask = cv2.inRange(hsv, lower1, upper1)
    output1 = cv2.bitwise_and(img, img, mask = mask)

for (lower2, upper2) in boundaries2:
    lower2 = np.array(lower2, dtype = "uint8")
    upper2 = np.array(upper2, dtype = "uint8")
    mask = cv2.inRange(hsv, lower2, upper2)
    output2 = cv2.bitwise_and(img, img, mask = mask)
for (lower3, upper3) in boundaries3:
    lower3 = np.array(lower3, dtype = "uint8")
    upper3 = np.array(upper3, dtype = "uint8")
    mask = cv2.inRange(hsv, lower3, upper3)
    output3 = cv2.bitwise_and(img, img, mask = mask)
for (lower4, upper4) in boundaries4:
    lower4 = np.array(lower4, dtype = "uint8")
    upper4 = np.array(upper4, dtype = "uint8")
    mask = cv2.inRange(hsv, lower4, upper4)
    output4 = cv2.bitwise_and(img, img, mask = mask)

final = cv2.bitwise_or(output1, output2, output3)
final1 = cv2.bitwise_or(output4, final)
cv2.imshow("final", final1)

while(1):
    k = cv2.waitKey(0)
    if(k == 27):
        break

cv2.destroyAllWindows()

How can we remove the yellow colour "parking" sign which is still showing in our output? Is it because we are using a jpeg image?

edit retag flag offensive close merge delete

Comments

please add the original, bgr image so folks here can try your (and alternative) ideas

berak gravatar imageberak ( 2017-11-21 12:18:42 -0600 )edit
1

Hi, I have added the original image if that is what you were asking for.

Camerutttt gravatar imageCamerutttt ( 2017-11-21 12:21:05 -0600 )edit

it is, thank you ;)

berak gravatar imageberak ( 2017-11-21 12:23:28 -0600 )edit

It seems that the rust is a dark red, and the parking sign is yellow-green. Can you use that difference in their properties as a way to exclude the sign?

sjhalayka gravatar imagesjhalayka ( 2017-11-22 09:34:26 -0600 )edit