0

I am trying to filter out a traffic light from the camera feed of my autonomous robot. I convert the captured frames into HSV colourspace, threshold them until I can get to detect all red objects that are roughly as "red" as the traffic light, then I apply Hough transform to find out the red light among them( which is obviously a circle).

Now the problem here is that I found that Hough transform only works with single channel images, and I seem to be working with three, so I tried converting the HSV thresholded image to Grayscale using the following code-

imgFinal = thresholded  #initialising the variable with the thresholded image.
framenew = cv2.cvtColor(threshNew, cv2.COLOR_HSV2GRAY)  

However during runtime I get an error saying 'module' object has no attribute 'COLOR_HSV2GRAY'. I found several questions regarding this topic but none were the answer to my problem. I only have to detect the red traffic light, I tried seperating the channels but I don't have any idea how to proceed with that approach. I am also open to any other approaches.
Thank you!

P.S- This might be off topic, but which language you all suggest for this project? C++ or Python? I've written the code for both, I'm just confused about which one to use. I'm currently developing it on my Windows 8.1 PC but it needs to run on the Raspberry Pi 3 for the final project.

  • typically you just use the V channel as the grayscale image, afaik. – Micka Mar 18 '16 at 22:15
  • Are you saying you are unsure how to split the channels ? If so, look at this: http://stackoverflow.com/questions/6699374/access-to-each-separate-channel-in-opencv . I would use Python if it is fast enough ... – IanB Mar 18 '16 at 22:40
  • @IanB That's in C++ and should work with my code (I can't test it right now). But the variables for storing channels are declared as Matrices, how do I do that in Python? I've planned to write the code for both languages and find by experiment which works better. –  Mar 19 '16 at 08:53
  • @IanB I tried splitting the channels but in Python that doesn't show up. I tried `h,s,v = cv2.split(image)` and then `cv2.imshow("V channel", v)` and I couldn't see the V channel window. Moreover if I try to implement it in C++ my program crashes during runtime. –  Mar 19 '16 at 09:26

1 Answers1

1

HSV's V channel is something like a grayscale image, but it is not identical to a BGR2GRAY grascale image.

This is a grayscale image computed from BGR2GRAY:

enter image description here

while this is the V channel of the HSV image:

enter image description here

If you need something "better", you can always convert your HSV image back to BGR and convert the BGR to GRAY with openCV codes.

Micka
  • 19,585
  • 4
  • 56
  • 74
  • I don't exactly need a Grayscale image, I just need some form of my Thresholded HSV image so that it could be used with Hough transform. I'll try converting the HSV to BGR and then to Grayscale and post back. –  Mar 19 '16 at 08:50
  • Hi! I tried converting the image from HSV to BGR. It has no error during compilation but when I run it my solution hangs (code in C++, made in Visual Studio Express 2012 for Windows desktop) –  Mar 19 '16 at 09:23
  • can you post your input image and your code how you converted to HSV and how you tried to convert to BGR? – Micka Mar 19 '16 at 09:30
  • I am working with a live camera feed from my webcam. There is no fixed image. Here is the link to the code, but I have removed the HSV to BGR part, would like you to suggest it. https://onedrive.live.com/redir?resid=EEABAA7CDCB08F75!29940&authkey=!APt3_wlaICxBsx4&ithint=file%2ctxt –  Mar 19 '16 at 12:52
  • you can save images from your live feed with cv::imwrite – Micka Mar 19 '16 at 12:53
  • Sorry for the late reply, I was facing internet issues. Anyways, the error message says that some module is missing. @Micka –  Mar 24 '16 at 18:05
  • Never mind I changed the position of the converting lines in the code and resolved the error. Problem is, the the image after conversion from HSV to BGR is exactly the same as the original image, which means when converted to grayscale it will be exactly as same as the original one converted to grayscale. So even if I detect the red circle I want to, how will I use the image with hough transform? @Micka –  Mar 25 '16 at 14:06
  • not sure what you want to achieve. maybe you want to use the H channel as input for houghTransform? Just usr cv::split and use the first channel. – Micka Mar 25 '16 at 14:10
  • I'm trying to find a way to use my thresholded image as input to hough transform to detect circles. Now out of the H,S and V channels, which one should be use if I am to split up channels? @Micka –  Mar 25 '16 at 17:44
  • did you try to jusr use the mask (thresholded image?) as input for hough? – Micka Mar 25 '16 at 18:30
  • I did... That was the first thing I did, but an exception was thrown saying that I need to input a 8 bit single channel image –  Mar 26 '16 at 13:54