-1

I tried to mask image by its color using opencv.

import cv2
import numpy as np
import matplotlib.pyplot as plt

After importing libraries, I load the image

img = cv2.imread('gmaps.jpg')
image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(image);

Turn the color into hsv

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
plt.imshow(hsv);

Masking process

low_orange = np.array([44, 6, 100])
high_orange = np.array([44, 24, 99])
masking = cv2.inRange(hsv,low_orange, high_orange)
plt.imshow(masking);

The result isn't what I expected.

Image : Real image

Result : Result

EDIT: I want to mask the building only. Instead I got the result of masking all of the frame.

Ceopee
  • 316
  • 4
  • 12
zzzz
  • 71
  • 7

2 Answers2

1

Using my answer from here I manage to extract the right values for you

enter image description here

Code:

    frame = cv2.imread("Xv6gx.png")
    blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
    hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)

    lower = np.array([4, 0, 7])
    upper = np.array([87, 240, 255])
    mask = cv2.inRange(hsv, lower, upper)
    contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    for contour in contours:
        area = cv2.contourArea(contour)
        if area > 5000:
             # -- Draw Option 1 --
             cv2.drawContours(frame, contour, -1, (0, 255, 0), 3)

             # -- Draw Option 2--
             # rect = cv2.boundingRect(contour)
             # x, y, w, h = rect
             # cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    cv2.imshow("Mask", mask)
    cv2.imshow("Frame", frame)

    cv2.waitKey(0)

Final Results:

Roy Amoyal
  • 717
  • 4
  • 14
  • How did you get the correct number for lower and upper orange colors? – zzzz Nov 01 '21 at 07:06
  • @zzzz In the beginning of my answer I hyperlink other answer of mine to find the colors. btw if you are pleased with my answer, can you upvote and verify (v symbol left to my answer) my answer? – Roy Amoyal Nov 02 '21 at 07:46
  • 1
    @RoyAmoial you can also try LAB colour space – Jeru Luke May 05 '22 at 20:55
0

I wouldn't expect the low Value (100) to exceed the high Value (99).

Also, OpenCV uses a range of 0..180 for Hue rather than 0..360, so you likely need to divide your 44 by 2.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432