如何在java opencv中将Blob转换为Mat?

时间:2016-02-11 10:39:52

标签: java opencv

我试图将MySQL数据库中的图像存储为blob并将它们转换为Mat对象以进行图像处理。我有一个模板图像,想要知道该图像是否是db图像的一部分,使用matchTemplate()函数。这是我的代码:

String sql="select * from image_data where image_id="+1
try
        {
            query obj_query=new query(obj_connect);
            ResultSet rs = obj_query.runSimpleQuery(sql);
            while (rs.next()) 
            {
                Blob image_blob=rs.getBlob("original_image");
                Mat img=Imgcodecs.imread(image_blob.toString());            
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
Mat templ=Imgcodecs.imread("templ.png");

findTemplate函数中传递这些图像:

MatchTemplate.findTemplete(img, templ, "ress.png", Imgproc.TM_SQDIFF);

这是我的findTemplate功能:

public static void findTemplete(Mat img, Mat templ, int match_method) 
     {
        double minVal;
        int result_cols = img.cols() - templ.cols() + 1;
        int result_rows = img.rows() - templ.rows() + 1;
        Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
        Imgproc.matchTemplate(img, templ, result, match_method);
        MinMaxLocResult mmr = Core.minMaxLoc(result);
        minVal = mmr.minVal;
        if(minVal<=0.75){
            //do what you want
        }
        else
        {
            //do what you want
        }
    }

当我运行它时,它会给我一个错误:

OpenCV Error: Assertion failed (s >= 0) in cv::setSize, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp, line 306
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp:306: error: (-215) s >= 0 in function cv::setSize
]
    at org.opencv.core.Mat.n_Mat(Native Method)
    at org.opencv.core.Mat.<init>(Mat.java:37)

它给我错误的行是:

Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

如何处理Blob图像以在Imagecodecs.imread()函数中传递它?或者是否有另一种技术可以将Blob转换为Mat

1 个答案:

答案 0 :(得分:0)

就这么简单。

byte[] decodedString = Base64.decode(imageBlob, Base64.DEFAULT);
Bitmap img = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Mat mat_img = new Mat();
Utils.bitmapToMat(img1, mat_img1);