gpt4 book ai didi

java - 错误,还是用户错误?在裁剪后的 Mat Image 上创建边框不会正确渲染最右边的边框线

转载 作者:搜寻专家 更新时间:2023-11-01 03:52:23 24 4
gpt4 key购买 nike

我正在为 OpenCv 使用 Java 绑定(bind),并尝试为已裁剪的图像添加边框。然而,虽然边框在完整图像上按预期绘制了 100%,但如果我裁剪源图像,它不会正确绘制左侧。

例如:不爽猫

enter image description here

现在,为了添加边框,我或多或少地复制了 OpenCV border tutorial 中的代码。 .

例如

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());
}
}
}

运行这段代码,我得到了一个漂亮的边框,每边都像预期的那样有 10px。

enter image description here

现在,如果我修改代码以首先裁剪图像,这将按预期停止工作。

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

有人知道这里发生了什么吗?

最佳答案

来自 Imgproc.copyMakeBorder javadoc:

When the source image is a part (ROI) of a bigger image, the function will try to use the pixels outside of the ROI to form a border. To disable this feature and always do extrapolation, as if src was not a ROI, use borderType BORDER_ISOLATED.

因此,如果您切换到 Imgproc.BORDER_ISOLATED 常量,它应该可以正常工作(确实如此):

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

关于java - 错误,还是用户错误?在裁剪后的 Mat Image 上创建边框不会正确渲染最右边的边框线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22272532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com