Click here to Skip to main content
15,886,079 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on project project that do some process on live video of camera using ICImagingControl in c# .net Framework 4.5,
i am recieving video and i want to do some image processing like Line detection. i have the function which is used for capturing and displaying images in a video stream.
here is the source code that i have.

C#
<pre> void icVideo_ImageAvailable(object sender, ICImagingControl.ImageAvailableEventArgs e)
        {

            try
            {
                Currentbuffer = e.ImageBuffer;
               
                icVideo.DisplayImageBuffer(Currentbuffer);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


What I have tried:

here is some code that i have tried but it throws runtime error.

and not getting what i intended to get(line detection on the image).


C#
<pre>void icVideo_ImageAvailable(object sender, ICImagingControl.ImageAvailableEventArgs e)
        {

            try
            {
                Currentbuffer = e.ImageBuffer;


                // Convert Currentbuffer to Mat
                Mat image = new Mat(Currentbuffer.BitsPerPixel, Currentbuffer.BitsPerPixel, (int)DepthType.Cv8U, Currentbuffer.BytesPerLine);

                // Convert image to grayscale
                Mat grayImage = new Mat();
                CvInvoke.CvtColor(image, grayImage, ColorConversion.Bgr2Gray);

                // Apply Canny edge detection algorithm
                double cannyThreshold = 180.0;
                double cannyThresholdLinking = cannyThreshold * 0.4;
                Mat cannyEdges = new Mat();
                CvInvoke.Canny(grayImage, cannyEdges, cannyThreshold, cannyThresholdLinking);

                // Apply Hough line detection algorithm
                double rho = 1.0; // distance resolution in pixels of the Hough grid
                double theta = Math.PI / 180.0; // angular resolution in radians of the Hough grid
                int threshold = 100; // minimum number of votes (intersections in Hough grid cell)
                double minLineLength = 100.0; // minimum number of pixels making up a line
                double maxLineGap = 10.0; // maximum gap in pixels between connectable line segments
                LineSegment2D[] lines = CvInvoke.HoughLinesP(cannyEdges, rho, theta, threshold, minLineLength, maxLineGap);


                // Draw lines on the image
                foreach (LineSegment2D line in lines)
                {
                    CvInvoke.Line(image, line.P1, line.P2, new MCvScalar(0, 0, 255), 2);
                }
                //icVideo.DisplayImageBuffer(new ImageBuffer(image.Width, image.Height, image.DataPointer, image.NumberOfChannels, image.Depth));
                // Display the processed image in the icVideo control
                icVideo.DisplayImageBuffer(Currentbuffer);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
}
Posted
Comments
Richard MacCutchan 13-Apr-23 3:03am    
"it throws runtime error."
What error and where does it occur?
sahil ajmeri 2022 13-Apr-23 3:35am    
getting nullbitmap on Mat image = new Mat(Currentbuffer.BitsPerPixel, Currentbuffer.BitsPerPixel, (int)DepthType.Cv8U, Currentbuffer.BytesPerLine);

so i am getting thow to cache from this line :
CvInvoke.CvtColor(image, grayImage, ColorConversion.Bgr2Gray);

error message is :
OpenCV: scn == 3 || scn == 4


may be i am doing something wrong in this line of code :
Mat image = new Mat(Currentbuffer.BitsPerPixel, Currentbuffer.BitsPerPixel, (int)DepthType.Cv8U, Currentbuffer.BytesPerLine);
Richard MacCutchan 13-Apr-23 4:02am    
Please show us the exact and complete error message.
sahil ajmeri 2022 13-Apr-23 5:53am    
https://ibb.co/V2gg2kF


here's link of error message image.
Richard MacCutchan 13-Apr-23 6:38am    
Sorry, that message does not help. You will need to check the documentation for that method. However,
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, - OpenCV Q&A Forum[^] may offer a clue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900