-3

I'm trying to filter red color in my images but I got an error which is cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed . I have no idea how can I fix this. Here is my code

import cv2
import numpy as np


path = "C:\\Users\\MERYEM\\OneDrive\\Masaüstü\\scan\\Img_Data\\Chosen\\frame3802.jpg"

image = cv2.imread(path)

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_red = np.array([178,179,0])
upper_red = np.array([255,255,255])

mask = cv2.inRange(hsv,lower_red, upper_red)

cv2.imshow('Original Image',image)
cv2.imshow('Detection', hsv)

cv2.waitKey(0)
cv2.destroyAllWindows()

I need you help, thanks in advance. Btw, I am using PyCharm if it matters

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Your code works fine for me. Are you sure you are using an RGB image, and not YUV or grayscale? And are you really sure those are legitimate HSV values? – Tim Roberts Aug 30 '21 at 21:26
  • The error message says that the image is not read. Make sure you have entered the correct path of the image. There is no other reason for this error message. – Rahul Kedia Aug 31 '21 at 04:14
  • HSV range for opencv is (0-180, 0-255, 0-255) by the way. – Shamshirsaz.Navid Aug 31 '21 at 07:32
  • @TimRoberts hey. I took this code from a youtube channel and I just edited the path. I'm sure that the path is correct however it does not work. Tim, which editor do you use? Maybe this is an editor issue, idk – Meryem Çiftçi Sep 01 '21 at 04:04
  • That's hard to imagine. [255,255,255] is not a valid HSV. Have you taken even the simplest debugging steps to print each and every value after you create it, to narrow things down? – Tim Roberts Sep 01 '21 at 06:52

1 Answers1

0

Check the image address again or select another image for test.

Also; HSV range for OpenCV is (0-180, 0-255, 0-255)

import cv2
import sys
import numpy as np


im = cv2.imread(sys.path[0]+'/back.png')

hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
redL = np.array([180*350/360, 0, 0])
redU = np.array([180*360/360, 255, 255])
mask = cv2.inRange(hsv, redL, redU)


mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
cv2.imwrite(sys.path[0]+'/out.png',np.hstack((im, mask)))

Output of image and output mask:

enter image description here


More information about modules and their licenses: OpenCV, NumPy

Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36