Error or user error? Creating a border with a cropped Mat Image does not display the correct line border correctly

I use Java bindings for OpenCvand try to add a border to the cropped image. However, while the border draws at 100%, as expected, in the full image, it does not draw the left side correctly if I crop the original image.

For example: Grumpy Cat

enter image description here

Now, to add a border, I more or less copied the code from the tutorial along the OpenCV border .

eg.

public class main {
   public static void main( String[] args )
   {
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("images\\original.jpg", Highgui.CV_LOAD_IMAGE_COLOR);


         Mat destination = new Mat(source.rows(),source.cols(),source.type());

         // make the border with a size of 10px for each side 
         Imgproc.copyMakeBorder(source, destination, 10, 10, 
                                10, 10, Imgproc.BORDER_CONSTANT, new Scalar(255,0,0));

         // Save the image
         Highgui.imwrite("images\\borderWrap.jpg", destination);
         }
         catch (Exception e) {
            System.out.println("error: " + e.getMessage());
         }
   }
}

By running this code, I get a nice border with 10px on each side, as expected.

enter image description here

Now, if I modify the code to crop the image first, it will stop working as expected.

public class main {
   public static void main( String[] args )
   {
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat isource = Highgui.imread("images\\original.jpg", Highgui.CV_LOAD_IMAGE_COLOR);

         // crop the image to half of its width
         Mat source = new Mat(isource, new Rect(0, 0, 480, 540)); 

         Mat destination = new Mat(source.rows(),source.cols(),source.type());

         // make the border with a size of 10px for each side 
         Imgproc.copyMakeBorder(source, destination, 10, 10, 
                                10, 10, Imgproc.BORDER_CONSTANT, new Scalar(255,0,0));

         // Save the image
         Highgui.imwrite("images\\borderWrap.jpg", destination);
         }
         catch (Exception e) {
            System.out.println("error: " + e.getMessage());
         }
   }
}

enter image description here

. , - ?

, copyMakeBorder, , , .

 // First border pass (doesn't draw right size)
 Imgproc.copyMakeBorder(source, destination, 10, 10, 
                                                10, 10, Imgproc.BORDER_CONSTANT, 
                                                new Scalar(255,0,0));

 //Second border pass -- only rightmost param is supplied a size      
 Imgproc.copyMakeBorder(destination, destination, 0, 0, 
                                                        0, 10, Imgproc.BORDER_CONSTANT, 
                                                        new Scalar(255,0,0));

enter image description here

- , ?

+4
1

Imgproc.copyMakeBorder javadoc:

(ROI) , ROI . , src ROI, borderType BORDER_ISOLATED.

, Imgproc.BORDER_ISOLATED, ( ):

Imgproc.copyMakeBorder(source, destination, 10, 10, 10, 10,
        Imgproc.BORDER_ISOLATED, new Scalar(255,0,0));
0

Source: https://habr.com/ru/post/1530761/


All Articles