Ask Your Question
0

How to find borders and crop the image

asked 2016-05-29 23:00:29 -0600

ChukkapalliSukhesh gravatar image

updated 2016-05-29 23:30:24 -0600

In my project, I am try to detect a handwritten character. How to find borders of image based on pixel colour and crop image. I wrote some code but it perfectly crops only at top left side written character. It cannot work proper for all images. My code is

    for (int x = 0; x < bt.getWidth(); x++) {
                    for (int y = 0; y < bt.getHeight(); y++) {




                        if (bt.getPixel(x, y) == Color.BLACK) {
                            if (xmin == 0 && xmax==0)
                                xmin = x;
                            if (ymin == 0 && ymax==0)
                                ymin = y;
                            if (x > xmax && x>xmin) {
                                xmax = x;
                            }
                            if (x < xmin && x < xmax) {
                                xmin = x;
                            }
                            if (y > ymax && y>ymin) {
                                ymax = y;
                            }
                            if (y < ymin && y < ymax) {
                                ymin = y;
                            }
                           // Log.i("pix bef","" + xmin + " " + ymin + " " + xmax + " " + ymax);
                            if((xmin+xmax)>bt.getWidth()){
                                xmax=bt.getWidth()-xmin;
                            }
                            if((ymin+ymax)>bt.getHeight()){
                                ymax=bt.getHeight()-ymin;
                            }
                        }

                    }
                }
                 Rect roi = new Rect(xmin, ymin, xmax, ymax);
                Mat cr = new Mat(mm, roi);

image description image description

The output is accurate at top left only. Getting much weight space at top right. Please solve how to eliminate extra weight space. Thank you.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-05-30 11:49:26 -0600

updated 2016-05-30 11:58:57 -0600

try this code.

i get these images ( to get second image uncomment commented lines )

image description image description

Note: with C++ the easiest way is to use findNonZero() and boundingRect() functions to achieve this like explained here

                Mat src = Imgcodecs.imread("d:/bt.png");
                Mat bt = new Mat();
                Imgproc.cvtColor(src, bt, Imgproc.COLOR_BGR2GRAY);
                Imgproc.threshold(bt, bt, 0, 255, Imgproc.THRESH_BINARY);
                Imgcodecs.imwrite("d:/bw.png",bt);

                int xmin = bt.cols();
                int ymin = bt.rows();
                int xmax=0;                
                int ymax=0;

                double[] pixel = new double[0];
                for (int x = 0; x < bt.cols(); x++) {
                    for (int y = 0; y < bt.rows(); y++) {

                        pixel = bt.get(y, x);

                        if( pixel[0] == 0)
                        {
                            if( x < xmin )
                            {
                                xmin = x;
                            }

                            if( y < ymin )
                            {
                                ymin = y;
                            }

                            if( x > xmax )
                            {
                                xmax = x;
                            }

                            if( y > ymax )
                            {
                                ymax = y;
                            }
                        }

                    }
                }
                //xmin = xmin - 3;  // you can try to uncomment these lines
                //ymin = ymin - 3;
                //xmax = xmax + 3;
                //ymax = ymax + 3;

                Rect roi = new Rect(xmin, ymin, xmax-xmin, ymax-ymin);
                Mat cr = new Mat(src, roi);
edit flag offensive delete link more

Comments

Thank you very much

ChukkapalliSukhesh gravatar imageChukkapalliSukhesh ( 2016-05-31 22:32:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-29 23:00:29 -0600

Seen: 8,512 times

Last updated: May 30 '16