Java根据x,y坐标为图片画红框,并返回新图片

文章介绍了如何使用Java的ImageIO和BufferedImage在给定路径的图片上绘制矩形框并保存结果。
摘要由CSDN通过智能技术生成
package com.oracle;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class WriteImageDemo {

    public static void main(String[] args) {
        // 图片路径
        String imagePath = "D:\\11.jpg"; // 替换为你的图片路径

        // 加载图片
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(imagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (image != null) {
            // 获取图片宽度和高度
            int width = image.getWidth();
            int height = image.getHeight();

            // 创建一个副本图像,在其上绘制框
            BufferedImage resultImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = resultImage.createGraphics();

            // 在副本图像上绘制原始图像
            g.drawImage(image, 0, 0, null);

            // 设置框的颜色和宽度
            g.setColor(Color.RED);
            g.setStroke(new BasicStroke(2)); // 设置线宽

            // 根据给定的 x 和 y 坐标绘制框
            int x = 100; // 替换为你的 x 坐标
            int y = 200; // 替换为你的 y 坐标
            int boxWidth = 200; // 框的宽度
            int boxHeight = 150; // 框的高度
            g.drawRect(x, y, boxWidth, boxHeight);

            g.dispose(); // 释放资源

            // 将绘制了框的图像保存为新的文件
            String outputImagePath = "D:\\image_with_box.jpg"; // 新图片保存路径
            try {
                ImageIO.write(resultImage, "jpg", new File(outputImagePath));
                System.out.println("New image with bounding box saved to: " + outputImagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Failed to load the image.");
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以!下面是一个用Java写的OpenCV模板匹配的示例代码: ```java import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfFloat; import org.opencv.core.MatOfInt; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.core.CvType; import org.opencv.core.CvType.CV_32F; import org.opencv.core.CvType.CV_8UC1; import org.opencv.core.CvType.CV_8U; import org.opencv.core.MatOfPoint; import org.opencv.core.MatOfPoint2f; import org.opencv.core.RotatedRect; import org.opencv.core.TermCriteria; import org.opencv.core.CvType; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class TemplateMatchingExample { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 读取源图像和模板图像 Mat sourceImage = Imgcodecs.imread("source_image.jpg", Imgcodecs.IMREAD_COLOR); Mat templateImage = Imgcodecs.imread("template_image.jpg", Imgcodecs.IMREAD_COLOR); // 创建结果矩阵,用于存储模板匹配结果 int resultWidth = sourceImage.cols() - templateImage.cols() + 1; int resultHeight = sourceImage.rows() - templateImage.rows() + 1; Mat result = new Mat(resultHeight, resultWidth, CvType.CV_32FC1); // 进行模板匹配 Imgproc.matchTemplate(sourceImage, templateImage, result, Imgproc.TM_CCOEFF); // 标记匹配结果 Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat()); Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(result); Point matchLoc = minMaxLocResult.maxLoc; // 绘制匹配结果矩形框 Rect matchRect = new Rect(matchLoc, new Size(templateImage.cols(), templateImage.rows())); Imgproc.rectangle(sourceImage, matchRect.tl(), matchRect.br(), new Scalar(0, 255, 0), 2); // 显示匹配结果 HighGui.imshow("Source Image", sourceImage); HighGui.waitKey(); // 输出匹配结果的位置坐标 System.out.println("匹配结果位置:(" + matchLoc.x + ", " + matchLoc.y + ")"); } } ``` 请确保你已经安装了OpenCV库,并将源图像和模板图像替换为你的实际图像路径。这段代码将显示源图像,并在找到的匹配位置绘制矩形框,并输出匹配结果的位置坐标。 希望对你有帮助!如果你有任何问题,请随时问我!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我向往自由

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值