shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.39k stars 1.15k forks source link

Histogram sample not working #609

Closed gnimor closed 5 years ago

gnimor commented 5 years ago

Summary of your issue

I am trying to run the histogram sample as shown on the wiki page with the latest OpenCvSharp4 package from NuGet. But I get an ArgumentException during the render operation of the histogram.

During the render part of the histogram I get an Argument exception: Message: bottom > top

    render.Rectangle(
        new OpenCvSharp.Point(j * binW, render.Rows),
        new OpenCvSharp.Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
        color,
        -1);

Environment

Windows 10

Example code:

                    Mat src = new Mat(@"E:\Downloads\lenna_grayscale.png", ImreadModes.Grayscale);

                    // Histogram view
                    const int Width = 260, Height = 200;
                    Mat render = new Mat(new OpenCvSharp.Size(Width, Height), MatType.CV_8UC3, Scalar.All(255));

                    // Calculate histogram
                    Mat hist = new Mat();
                    int[] hdims = { 256 }; // Histogram size for each dimension
                    Rangef[] ranges = { new Rangef(0, 256), }; // min/max 
                    Cv2.CalcHist(
                        new Mat[] { src },
                        new int[] { 0 },
                        null,
                        hist,
                        1,
                        hdims,
                        ranges);

                    // Get the max value of histogram
                    double minVal, maxVal;
                    Cv2.MinMaxLoc(hist, out minVal, out maxVal);

                    Scalar color = Scalar.All(100);
                    // Scales and draws histogram
                    hist = hist * (maxVal != 0 ? Height / maxVal : 0.0);
                    for (int j = 0; j < hdims[0]; ++j)
                    {
                        int binW = (int)((double)Width / hdims[0]);
                        render.Rectangle(
                            new OpenCvSharp.Point(j * binW, render.Rows),
                            new OpenCvSharp.Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
                            color,
                            -1);
                    }

                    using (new Window("Image", WindowMode.AutoSize | WindowMode.FreeRatio, src))
                    using (new Window("Histogram", WindowMode.AutoSize | WindowMode.FreeRatio, render))
                    {
                        Cv2.WaitKey();
                    }

Output:

Message: bottom > top

Stack Trace:

   at OpenCvSharp.Rect.FromLTRB(Int32 left, Int32 top, Int32 right, Int32 bottom)
   at OpenCvSharp.Cv2.Rectangle(Mat img, Point pt1, Point pt2, Scalar color, Int32 thickness, LineTypes lineType, Int32 shift)
   at OpenCvSharp.Mat.Rectangle(Point pt1, Point pt2, Scalar color, Int32 thickness, LineTypes lineType, Int32 shift)
   at HPD.Collections.Interfaces.Buddies.BuddyConData2D.MPictureBox_MouseClick(Object sender, MouseEventArgs e) in E:\Code\Kaishin\Libraries\HPDBuddies\BuddyConData2D.cs:line 303
   at System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Data2DGeneratorStart.Program.Main() in E:\Code\Kaishin\Test\FUs\Get2DDataFromDCAM\ProjStarter\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
AlucardNosferatu commented 5 years ago

I encountered the same issue as you

AlucardNosferatu commented 5 years ago

While I exchange 2 points and the program works with blank Histo result image

ffrankozz commented 5 years ago

In the sample, the Rectangle object coordinates or the points are in the wrong order. If you correct them, you will get it working. Just think about how you would draw a vertical bar or rectangle.

AlucardNosferatu commented 5 years ago

Yes, Frank is right, I change The order OF X coordinate and output right histo

shimat commented 5 years ago

Thanks. I fixed this issue https://github.com/shimat/opencvsharp_samples/compare/523359f9981a2e7248d11ccb2813c32a9c2b9202...1975cbeedade46a1e392381e8848341a65a2f255#diff-65c262f86fa324fd80ac0aa3919ac003