Imgproc.cvtColor(img, grayImg, Imgproc. gray cv2.cvtColor(frame, cv2.COLORBGR2GRAY) 11 12 ret, gb cv2.threshold(gray,128,255,cv2.THRESHBINARY) 13 14 gb cv2.bitwisenot(gb) 15 16 contour,hier cv2.findContours(gb,cv2.RETRCCOMP,cv2.CHAINAPPROXSIMPLE) 17 18 for cnt in contour: 19 cv2.drawContours(gb, cnt,0,255,-1) 20 gray cv2. GrayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) Grayscale value is calculated as weighted sum of the R, G, and B colour components. OpenCV uses weighted method, also known as luminosity method for RGB to grayscale conversion. Note that, OpenCV loads an image where the order of the color channels is Blue, Green, Red (BGR) instead of RGB. BGR2GRAY code is used to convert RGB image to grayscale image. IMREADGRAYSCALE, the image is read as grey scale image. Use img cv2.imread (filename, 0) to make sure img is a 1-channel image. You got the error because you didn't assign it to return a gray image. Input can be color image or grey scale image. 1 img cv2.imread (filename, flags) returns a 3-channel color image when flag>0 returns a gray image when flag0 returns a image as the loaded file originally is. ![]() This function accepts color conversion code. Example 2: OpenCV cv2 Read Image as Grey Scale In this example, we will read image as a grey scale image. OpenCV provides cvtColor function that allows to convert an image from one color space to another. This tutorial provides example how to convert RGB image to grayscale image using OpenCV. ![]() During image processing RGB images are often converted to grayscale images because smaller amount of data allows to perform more complex image processing operations faster.
0 Comments
Leave a Reply. |