How to convert Blob to Mat in java opencv? - java

I am trying to get images from MySQL database stored as blob and convert them into Mat object for image processing. I have a template image and want to know if that image is part of db image or not using matchTemplate() function. Here is my code:
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");
Passing these images in findTemplate function:
MatchTemplate.findTemplete(img, templ, "ress.png", Imgproc.TM_SQDIFF);
And here is my findTemplate function:
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
}
}
When I run it, it gives me an error:
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)
Line on which it gives me the error is:
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
How can I handle Blob image to pass it in Imagecodecs.imread() function? Or is there another technique to convert Blob to Mat?

Simple as this.
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);

Related

javax.imageio.IIOException: Missing Huffman code table entry while Adding text to an jpg image

I am adding text to an image using this code in Android :
public Bitmap drawTextToBitmap(Context gContext, Bitmap image, String gText) {
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
android.graphics.Bitmap.Config bitmapConfig =
image.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
Bitmap bitmap = null;
try{
bitmap = image.copy(bitmapConfig, true);
image.recycle();
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.WHITE);
// text size in pixels
paint.setTextSize((int) (50 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
int padding = bounds.height()/2;
int x = bitmap.getWidth() - (bounds.width()+padding);
int y = (bitmap.getHeight() - (bounds.height()+padding));
canvas.drawText(gText, x, y, paint);
}catch (Throwable e){
AppLog.e("DrawingBitmap","error while adding timestamp",e);
}
return bitmap;
}
Then I create a new File with the transformed bitmap
storeImage(newBitmap, newFileName);
private File storeImage(Bitmap image, String nameFile) {
File pictureFile = new File(getExternalCacheDir(), nameFile);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
AppLog.e("error creating bitmap", "File not found: " + e.getMessage());
} catch (IOException e) {
AppLog.e("error creating bitmap", "Error accessing file: " + e.getMessage());
}
return pictureFile;
}
I send the file to my server, I receive an input stream, I create a File, I scale it and I create a new File with the scaled image :
ImageWriter.write(metadata, new IIOImage(image, null, metadata), param);
I get an IIOException:
javax.imageio.IIOException: Missing Huffman code table entry
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1067)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:363)
at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:162)
if I don't call drawTextToBitmap() from android I don't get that error.
If someone can help me ... thx
EDIT : here is the way I use to get metadata from my file
private static IIOMetadata readMetaData(File source) {
try {
ImageInputStream stream = ImageIO.createImageInputStream(source);
Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
IIOMetadata metadata = null;
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(stream);
metadata = reader.getImageMetadata(0);
}
return metadata;
}catch (Exception e){
_logger.error(e);
e.printStackTrace();
}
return null;
}
Edit 2 :
Using jpegParams.setOptimizeHuffmanTables(true); works but it resets all metadata and I want to keep them like gps location ...
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
if (param instanceof JPEGImageWriteParam) {
((JPEGImageWriteParam) param).setOptimizeHuffmanTables(true);
}
IIOImage image = new IIOImage(reader.read(0), null, reader.getImageMetadata(0));
writer.write(null, image, param);
here's my code, which keeps the metadata of image, and get rid of "missing huffman code table" stuff.

Converting JPG image to Mat opencv UnsupportedOperationException

I'm trying to convert a buffered image to a Matrix, but it throws an UnsupportedOperationException, which I have never seen before
public static Mat readMatImage(String path) {
Mat mat = null;
BufferedImage image;
try {
image = ImageIO.read(new FileInputStream(path));
mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.out.println(data[data.length - 1]);
mat.put(0, 0, data);
} catch (IOException e) {
e.printStackTrace();
}
return mat;
}
Exception in thread "main" java.lang.UnsupportedOperationException: Provided data element number (4000000) should be multiple of the Mat channels count (3)
I think you should consider your read-image type because your image must be single channel and 8-bit to cast it to a mat element. If your image is RGB, try to convert it to binary image.

OpenCV color analysis in HSV

As the title suggests, I am interested in getting the HSV value of a specific pixel using java CV. This sounds easy enough, and it seems to be straight forward in C++ or Python, but I simply cant figure out how to do it in Java. I am pretty new to OpenCV, and if I decide to do more projects using this library I will definitely write them in C++ or Python.
For reference, my goal is to do a color analysis of an object that has varying levels of lighting. The end goal is to be able to take an image of something like a t-shirt and be able to say "this t shirt is x% red".
Here is some of the code I was using. Surprisingly inRange() takes much longer than just looping through every pixel and getting RGB one by one. I want to be able to do exactly this, just with the HSV color space. If you know of a better way to accomplish this goal, please let me know as this has destroyed my entire Saturday. Thanks!
Scalar min = new Scalar(22,11,3);
Scalar max = new Scalar(103,87,74);
int sum = 0;
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
File input = new File("bluesample.jpg");
BufferedImage image = ImageIO.read(input);
byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3);
mat.put(0, 0, data);
Core.inRange(mat, min, max, mat1);
System.out.println(mat1.total());
System.out.println(mat1.total());
for (int i=0;i<mat1.rows(); i++ ){
for (int j=0;i<mat1.cols();j++){
sum += mat1.get(j, i, data);
}
}
System.out.println(sum/mat1.total());
EDIT:
try { System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
File input = new File("singlehsvpix.jpg");
BufferedImage image = ImageIO.read(input);
byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
mat.put(0, 0, data);
Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC1);
Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2HSV);
System.out.println(mat1.dump());
byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int)(mat1.elemSize())];
mat1.get(0, 0, data1);
//BufferedImage image1 = new BufferedImage(mat1.cols(),mat1.rows(), BufferedImage.TYPE_BYTE_GRAY);
BufferedImage image1 = new BufferedImage(mat1.cols(),mat1.rows(), 5);
image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);
File output = new File("PLS!.jpg");
ImageIO.write(image1, "jpg", output);
System.out.println(mat1.get(0, 0, data1)); // RELEVANT LINE
System.out.println("Done");
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
Is printing:
[ 54, 213, 193]
3
Done
For this pic, 54, 213, 193 are the BGR values... I guess I don't understand enough about OpenCV to know why my mat1.get is printing 3
So, you want to convert rgb to hsv.
Imgproc.cvtColor(im_rgb, im_hsv, Imgproc.COLOR_RGB2HSV);
Then, process as you like
Edit: in your code, change mat to mat1
for (int i=0;i<mat1.rows(); i++ ){
for (int j=0;i<mat1.cols();j++){
sum += mat.get(j, i, data); //this line
}
}
System.out.println(sum/mat1.total());
You are adding the value in original matrix.

OpenCV InputStream to Mat type and then templatematching, works with "jpg" but not with "png"

I would like to grab 2 images from my mysql database, one source image and one template image and then do template matching. When the source file on database is jpg below methods work but when it is png they dont work. The weird thing is it works when the template file is png, but when source is png it doesnt work. On the other hand at my local computer, when I dont use inputstream, no matter which one is png/jpg it always works.
//Here is how I get the images:
stmt = conn.createStatement();
String query = "select id, template_pic, source_pic from my_table";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
int ID_pics = rs.getInt(1);
//picture Code template matching
InputStream temp_file = rs.getBinaryStream(2);
InputStream inFile = rs.getBinaryStream(3);
new MatchingDemo().run(inFile, temp_file, "Out_image"+ ID_pics +".png", Imgproc.TM_CCOEFF_NORMED);
Thread.sleep(5000);
}
// Here is on MatchingDemo() class the conversion to Mat:
private static byte[] readStream(InputStream stream) throws IOException {
// Copy content of the image to byte-array
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[524288]; // 16384
while ((nRead = stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] temporaryImageInMemory = buffer.toByteArray();
buffer.close();
stream.close();
return temporaryImageInMemory;
}
byte[] temporaryImageInMemory = readStream(inFile);
byte[] temporaryImageInMemory_temp = readStream(templateFile);
Mat img = Highgui.imdecode(new MatOfByte(temporaryImageInMemory), -1);
Mat templ = Highgui.imdecode(new MatOfByte(temporaryImageInMemory_temp), -1);
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);
// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method); // This line is where I get the error and the error message is as below:
// Error message I get from Eclipse
OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in cv::matchTemplate, file ..\..\..\..\opencv\modules\imgproc\src\templmatch.cpp, line 249
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\imgproc\src\templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function cv::matchTemplate
]
at org.opencv.imgproc.Imgproc.matchTemplate_0(Native Method)
at org.opencv.imgproc.Imgproc.matchTemplate(Imgproc.java:7621)
at MatchingDemo.run(MatchingDemo.java:104)
I would be glad if you can give a hand.

Training SVM bad argument error

I followed the code in How to train an SVM with opencv based on a set of images? and I get the following exception
OpenCV Error: Bad argument (train data must be floating-point matrix) in cvCheckTrainData, file ......\src\opencv\modules\ml\src\inner_functions.cpp, line 857
Exception in thread "main" CvException [org.opencv.core.CvException: ......\src\opencv\modules\ml\src\inner_functions.cpp:857: error: (-5) train data must be floating-point matrix in function cvCheckTrainData
]
I followed the steps as mentioned in the comment. But still in vain.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat classes = new Mat();
Mat trainingData = new Mat();
Mat trainingImages = new Mat();
Mat trainingLabels = new Mat();
CvSVM clasificador;
String path="C:\\java workspace\\ora\\images\\Color_Happy_jpg";
for (File file : new File(path).listFiles()) {
Mat img=new Mat();
Mat con = Highgui.imread(path+"\\"+file.getName());
con.convertTo(img, CvType.CV_32F,1.0/255.0);
img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32F));
}
System.out.println("divide");
path="C:\\java workspace\\ora\\images\\Color_Sad_jpg";
for (File file : new File(path).listFiles()) {
Mat img=new Mat();
Mat m=new Mat(new Size(640,480),CvType.CV_32FC3);
Mat con = Highgui.imread(file.getAbsolutePath());
con.convertTo(img, CvType.CV_32F,1.0/255.0);
img.reshape(1, 1);
trainingImages.push_back(img);
System.out.println((CvType.typeToString(img.type())));
trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32F));
}
trainingLabels.copyTo(classes);
CvSVMParams params = new CvSVMParams();
params.set_kernel_type(CvSVM.LINEAR);
System.out.println(CvType.typeToString(trainingImages.type()));
CvSVM svm=new CvSVM();
System.out.println(svm.get_support_vector_count());
boolean b=svm.train(trainingImages, classes);
System.out.print(b);
}
}

Categories