java opencv 读取图片生成MAT

    
     /**
     * 读取图片生成矩阵MAT
     * @param imgPath   图片路径
     * @return
     */
    public static Mat converMat(String imgPath){
        return Imgcodecs.imdecode(new MatOfByte(fileToBytes(imgPath)), 
                  Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
    }
    
    
    
    /**
     * 读取图片字节流
     * @param filePath 
     * @return
     */
    public static byte[] fileToBytes(String filePath) {
        byte[] buffer = null;
        File file = new File(filePath);
        
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;

        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();

            byte[] b = new byte[2048];

            int n;

            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            
            buffer = bos.toByteArray();
        } catch (FileNotFoundException ex) {
           
        } catch (IOException ex) {
            
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException ex) {
               
            } finally{
                try {
                    if(null!=fis){
                        fis.close();
                    }
                } catch (IOException ex) {
                    
                }
            }
        }
        
        return buffer;
    }

如果直接用 Imgcodecs.imread(imgPath)函数,可能会遇到出错,所以先把图片转成byte[],再由byte转成Mat。

 古智云开 您的个人图书馆

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,以下是使用JavaOpenCV进行亮度剔除的示例代码: ```java import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfFloat; import org.opencv.core.MatOfInt; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class BrightnessRemoval { public static void main(String[] args) { // 加载OpenCV库 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 读取图像 Mat image = Imgcodecs.imread("input.jpg"); // 将图像转换为灰度图像 Mat grayImage = new Mat(); Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY); // 计算图像的直方图 Mat hist = new Mat(); Imgproc.calcHist( new MatOfMat(grayImage), new MatOfInt(0), new Mat(), hist, new MatOfInt(256), new MatOfFloat(0, 256) ); // 计算亮度阈值 int threshold = calculateThreshold(hist); // 应用亮度阈值 Mat thresholdedImage = new Mat(); Imgproc.threshold(grayImage, thresholdedImage, threshold, 255, Imgproc.THRESH_BINARY); // 保存结果图像 Imgcodecs.imwrite("output.jpg", thresholdedImage); } private static int calculateThreshold(Mat hist) { int totalPixels = hist.rows() * hist.cols(); float sum = 0; for (int i = 0; i < hist.rows(); i++) { float binValue = (float) hist.get(i, 0)[0]; sum += i * binValue; } float sumB = 0; int wB = 0; int wF = 0; float varMax = 0; int threshold = 0; for (int i = 0; i < hist.rows(); i++) { wB += hist.get(i, 0)[0]; if (wB == 0) { continue; } wF = totalPixels - wB; if (wF == 0) { break; } sumB += i * hist.get(i, 0)[0]; float mB = sumB / wB; float mF = (sum - sumB) / wF; float varBetween = wB * wF * (mB - mF) * (mB - mF); if (varBetween > varMax) { varMax = varBetween; threshold = i; } } return threshold; } } ``` 请注意,上述代码假设您已经正确配置了JavaOpenCV,并且将输入图像命名为"input.jpg"。代码将生成一个二进制图像,其中亮度高于阈值的像素将被设置为255,于阈值的像素将被设置为0,并将结果保存为".jpg"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

古智云开

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

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

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

打赏作者

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

抵扣说明:

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

余额充值