Skip to content

Inrange method is invalid for cropped images #608

@liam31012

Description

@liam31012

Summary of your issue

Inrange method is useless for the image that use DrawImage method

Environment

win10+vs2017+opencvsharp4.0.0

Example code:

cut

paste your core code
`
using OpenCvSharp;
using OpenCvSharp.Extensions;
using static OpenCvSharp.Cv2;
using System.Drawing;
namespace RcoloR
{
    class Program
    {
        static Bitmap Mybmp = new Bitmap("E:/CVIMG/lenna.png");
        static void Main(string[] args)
        {
            using (new Window("SRC", WindowMode.Normal, Mybmp.ToMat())) ;   //source image

            Prtsc(Mybmp, out Bitmap dst);  //crop image           
            using (new Window("IMAGE_CUT", WindowMode.Normal, dst.ToMat())) ; //show the cropped image

            MyInrange(dst,out Bitmap dstbmp);   // Inrange 
            using (new Window("DST", WindowMode.Normal, dstbmp.ToMat()));  //show the inranged image

            WaitKey();
        }
        static void Prtsc(Bitmap src, out Bitmap dst)  // crop image
        {
            dst = new Bitmap(300,300);
            using (Graphics g = Graphics.FromImage(dst))
            {
                g.DrawImage(src,new Rectangle(0,0,300,300),new Rectangle(0,0,300,300),GraphicsUnit.Pixel);
            }
        }
        static void MyInrange(Bitmap bmpsrc,out Bitmap dstbmp) // Cv2.Inrange();
        {
            using (Mat src = BitmapConverter.ToMat(bmpsrc))
            using (Mat dst = new Mat())
            {
                Scalar low_value = new Scalar(100, 100, 100);
                Scalar high_value = new Scalar(255, 255, 255);
                InRange(src, low_value, high_value, dst);
                dstbmp = dst.ToBitmap();
            }
        }
    }
}
`


## Output:

paste your output

t3

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions