Laser line detection using OpenCV

I am working on a project in which I need to detect a red laser line in an image. This is the strategy that I have in mind.

  • Separate the channels R, G, B in the image.
  • Threshold image with a high intensity value.
  • Using the generated 3 binary images, perform the wise operation element r && g & &! B. (& & logically AND ,! logically NOT).
  • The resulting matrix is โ€‹โ€‹a binary image with 1 in the areas where the laser was present.

This worked with several test images on Matlab. But my problem is that this needs to be implemented using OpenCV in C / C ++.

I tried to use most of the library's functions, but there seems to be no intuitive / easy way to work with binary images and perform logical operations on them.

Can someone point me to OpenCV functions / methods that you think I can find useful? I figured cvThresholdImage could be used for a threshold value, but that is pretty much about it.

+4
source share
1 answer

So, have you already covered steps 1 and 2 in openCV? If you're just trying to use logical operators, openCV gives you access to raw data that you can work with logical operators. Assuming you are already divided into three channels and a threshold value

 //three binary images in the format you specified above cv::Mat g; cv::Mat b; cv::Mat r; uchar* gptr = g.data(); uchar* bptr = b.data(); uchar* rptr = r.data(); //assuming the matrix data is continuous you can just iterate straight through the data if(g.isContinuous()&&r.isContinuous()&&b.isContinuous()) { for(int i = 0; i < g.rows*g.cols; i++) { rptr[i] = rptr[i]&&!bptr[i]&&!gptr[i]; } } 

r now contains your output. You can also copy it to a new matrix if you do not want to overwrite r.

There are several ways to iterate through cv :: Mat and access all data points, while C ++ provides all the logical operators you might want. As far as I know, openCV does not provide matrix logical operator functions, but you can write your own very easily, as shown above.

Edit As suggested by QuentinGeissmann, you can accomplish the same thing using the bitwise_not and bitwise_and functions. I did not know that they exist. I suspect that using them will be slower due to how many times the data needs to be iterated, but this can be done in less code.

 cv::bitwise_not(g,g); cv::bitwise_not(b,b); cv::bitwise_and(b,g,b); cv::bitwise_and(r,b,r); //r now contains r&&!b&&!g 
+3
source

All Articles