1

I can't get a grayscale image in RGB, here's my code:

img=cv2.imread("clahe_2.jpg")
backtorgb = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
showImg(backtorgb,"claheCLR")

This error returns:

backtorgb = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
cv2.error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp:9765: error: (-215) scn == 1 && (dcn == 3 || dcn == 4) in function cvtColor

Filepath is correct. Any suggest?

Da Nio
  • 302
  • 1
  • 6
  • 16
  • 2
    1) If you want `imread()` to read in greyscale, you should set the flag to `cv2.IMREAD_GRAYSCALE` on `imread()`. 2) If you want to revert to a colour image, you should be aware that OpenCV uses BGR ordering in general. – Mark Setchell Jul 18 '19 at 13:10
  • Kindly print `img.shape` so we can see the number of channels in the image. – Mark Setchell Jul 18 '19 at 13:15

3 Answers3

1

I think tat problem is that your image is not real grayscale. It is RGB, but visible as grayscale. So need to bring one channel from image and then run the code:

backtorgb = cv2.cvtColor(img[..., 0], cv2.COLOR_GRAY2RGB)

Grzegorz Bokota
  • 1,736
  • 11
  • 19
0

By default, imread opens the image as a 3-channel BGR image so you don't need to convert it, maybe just to RGB if that's what you're looking for.

Check out the documentation here

gilad
  • 426
  • 1
  • 5
  • 15
0

The problem was on the application of the CLAHE filter and its grayscale output, actually the output kept the 3 channels but at the sight it looked like a grayscale, documentation here.

I simply found here a method that kept the RGB form to solve the problem, thanks to everyone for the answers.

Da Nio
  • 302
  • 1
  • 6
  • 16