How to identify and fill I shape Contours in javacv? - java

I'm developing project on javacv and I need to know how to identify following image and fill that Image using particular color ?
I try to go through this question and This is the image that I use
I try to go through this code and I developed a code in javacv
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import javax.swing.JFileChooser;
public class PolyGonIdentification {
public static void main(String[] args) {
CanvasFrame cnvs=new CanvasFrame("Polygon");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvCvtColor(src, gry, CV_BGR2GRAY);
cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV);
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
System.out.println(squares.total());
for (int i=0; i<squares.total(); i++)
{
cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
}
IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null);
cvDilate(gry, gry, mat, CV_C);
cvErode(gry, gry, mat, CV_C);
cnvs.showImage(gry);
}
}
My final result should be like this image
Put above code resulted this kind of image. Please can some one help me to resolve this issue ?

You can archive that using this code
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import javax.swing.JFileChooser;
public class Ishape {
public static void main(String[] args) {
CanvasFrame cnvs=new CanvasFrame("Polygon");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvCvtColor(src, gry, CV_BGR2GRAY);
cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV);
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
CvSeq ss=null;
for (int i=0; i<1; i++)
{
cvDrawContours(gry, squares, CvScalar.WHITE, CV_RGB(248, 18, 18), 1, -1, 8);
ss=cvApproxPoly(squares, sizeof(CvContour.class), storage, CV_POLY_APPROX_DP, 8, 0);
}
IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null);
cvDilate(gry, gry, mat, CV_C);
cvErode(gry, gry, mat, CV_C);
cnvs.showImage(gry);
}
}
Result
This will be resulted out put and I believe this might help you to solve your problem.

You just have to find contours using external retrieval mode:
CV_RETR_EXTERNAL retrives only the extreme outer contours
In your situation this is the best mode because you have one external contour and this contour is what you're looking for. Here's documantation.
After this just draw it using drawContours with CV_FILLED as thickness parameter to fill external polygon.

The code does exactly what is supposed to do:
// as stated [in the answer to your previous question][1], findContours leaves gry set to 0, or black
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
System.out.println(squares.total()); // nothing interesting
for (int i = 0; i < squares.total(); i++)
{
// draw a gray contour (127) on a black image
cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
}
To correct it, you have to set gry to 255 (white, before any call to drawContours, and draw black contours! that is 0, not 127.)

Related

OpenCV library in Tomcat(8.5.32) Server unable to execute

Facing issue while image processing code setup. In spite of doing all code changes and different approaches facing the issue.
Libraries used – OpenCV – 3.4.2
Jars Used – opencv-3.4.2-0
tess4j-3.4.8
Lines added in pom.xml
<!-- https://mvnrepository.com/artifact/org.openpnp/opencv -->
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.4.2-0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.4.8</version>
</dependency>
Steps for OpenCV installation :
Download opencv.exe from the official site’
Run opencv.exe, it will create an opencv folder
We have now the opencv library available which we can use for eclipse.
Steps for Tesseract installation :
Download tess4j.zip file from the official link
Extract the zip folder after download
Provide the path of the tess4j folder
Following are the steps which we have performed for the setup in eclipse :
We have added native library by providing path to openCV library from build path settings
We downloaded tesseract for image reading.
We provided the path to the Tesseraact in the code
We have used System.loadlibrary(Core.NATIVE_LIBRARY_NAME) and openCv.loadLocally() for loading the library.
Then we have made the WAR export for deployment
There has been no changes or setup in apache tomcat
For loading the libraries in Tomcat we have to provide some setup here :-
Now for the new code we have used, Load Library static class in the code (as solutions stated on stack overflow)
In here System.loadLibrary is not working
We had to use System.load along with hardcoded path which is resulting in internal error
We have used System.load – 2 time in the static class out of which the when the 1st one is giving -std error -bad allocation
As there are 2 paths in opencv-
This is the 1st one
System.load("C:\Users\Downloads\opencv\build\bin\opencv_java342.dll");
and the 2nd one is giving the assertion error based on which one is kept above
This is the 2nd one
System.load("C:\User\Downloads\opencv\build\java\x64\opencv_java3412.dll");
The code is executing till mid-way and then getting out and till now not yet code has reached till tesseract.
Here is the code for the same :
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.opencv.core.Core;
import org.opencv.core.CvException;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import net.sourceforge.tess4j.Tesseract;
import nu.pattern.OpenCV;
public class ReadImageBox {
public String readDataFromImage(String imageToReadPath,String tesseractPath)
{
String result = "";
try {
String i = Core.NATIVE_LIBRARY_NAME;
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
logger.info("Img to read "+imageToReadPath);
String imagePath =imageToReadPath; // bufferNameOfImagePath = "";
logger.info(imagePath);
/*
* The class Mat represents an n-dimensional dense numerical single-channel or
* multi-channel array. It can be used to store real or complex-valued vectors
* and matrices, grayscale or color images, voxel volumes, vector fields, point
* clouds, tensors, histograms (though, very high-dimensional histograms may be
* better stored in a SparseMat ).
*/
logger.info("imagepath::"+imagePath);
OpenCV.loadLocally();
logger.info("imagepath::"+imagePath);
//logger.info("Library Information"+Core.getBuildInformation());
logger.info("imagepath::"+imagePath);
Mat source = Imgcodecs.imread(imagePath);
logger.info("Source image "+source);
String directoryPath = imagePath.substring(0,imagePath.lastIndexOf('/'));
logger.info("Going for Image Processing :" + directoryPath);
// calling image processing here to process the data from it
result = updateImage(100,20,10,3,3,2,source, directoryPath,tesseractPath);
logger.info("Data read "+result);
return result;
}
catch (UnsatisfiedLinkError error) {
// Output expected UnsatisfiedLinkErrors.
logger.error(error);
}
catch (Exception exception)
{
logger.error(exception);
}
return result;
}
public static String updateImage(int boxSize, int horizontalRemoval, int verticalRemoval, int gaussianBlur,
int denoisingClosing, int denoisingOpening, Mat source, String tempDirectoryPath,String tesseractPath) throws Exception{
// Tesseract Object
logger.info("Tesseract Path :"+tesseractPath);
Tesseract tesseract = new Tesseract();
tesseract.setDatapath(tesseractPath);
// Creating the empty destination matrix for further processing
Mat grayScaleImage = new Mat();``
Mat gaussianBlurImage = new Mat();
Mat thresholdImage = new Mat();
Mat morph = new Mat();
Mat morphAfterOpreation = new Mat();
Mat dilate = new Mat();
Mat hierarchy = new Mat();
logger.info("Image type"+source.type());
// Converting the image to gray scale and saving it in the grayScaleImage matrix
Imgproc.cvtColor(source, grayScaleImage, Imgproc.COLOR_RGB2GRAY);
//Imgproc.cvtColor(source, grayScaleImage, 0);
// Applying Gaussain Blur
logger.info("source image "+source);
Imgproc.GaussianBlur(grayScaleImage, gaussianBlurImage, new org.opencv.core.Size(gaussianBlur, gaussianBlur),
0);
// OTSU threshold
Imgproc.threshold(gaussianBlurImage, thresholdImage, 0, 255, Imgproc.THRESH_OTSU | Imgproc.THRESH_BINARY_INV);
logger.info("Threshold image "+gaussianBlur);
// remove the lines of any table inside the invoice
Mat horizontal = thresholdImage.clone();
Mat vertical = thresholdImage.clone();
int horizontal_size = horizontal.cols() / 30;
if(horizontal_size%2==0)
horizontal_size+=1;
// showWaitDestroy("Horizontal Lines Detected", horizontal);
Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
new org.opencv.core.Size(horizontal_size, 1));
Imgproc.erode(horizontal, horizontal, horizontalStructure);
Imgproc.dilate(horizontal, horizontal, horizontalStructure);
int vertical_size = vertical.rows() / 30;
if(vertical_size%2==0)
vertical_size+=1;
// Create structure element for extracting vertical lines through morphology
// operations
Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
new org.opencv.core.Size(1, vertical_size));
// Apply morphology operations
Imgproc.erode(vertical, vertical, verticalStructure);
Imgproc.dilate(vertical, vertical, verticalStructure);
Core.absdiff(thresholdImage, horizontal, thresholdImage);
Core.absdiff(thresholdImage, vertical, thresholdImage);
logger.info("Vertical Structure "+verticalStructure);
Mat newImageFortest = thresholdImage;
logger.info("Threshold image "+thresholdImage);
// applying Closing operation
Imgproc.morphologyEx(thresholdImage, morph, Imgproc.MORPH_CLOSE, Imgproc.getStructuringElement(
Imgproc.MORPH_RECT, new Size(denoisingClosing, denoisingClosing)));
logger.info("Morph image "+morph);
// applying Opening operation
Imgproc.morphologyEx(morph, morphAfterOpreation, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(
Imgproc.MORPH_RECT, new Size(denoisingOpening, denoisingOpening)));
logger.info("Morph After operation image "+morphAfterOpreation);
// Applying dilation on the threshold image to create bounding box edges
Imgproc.dilate(morphAfterOpreation, dilate,
Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(boxSize, boxSize)));
logger.info("Dilate image "+dilate);
// creating string buffer object
String text = "";
try
{
// finding contours
List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); // A list to store all the contours
// finding contours
Imgproc.findContours(dilate, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
logger.info("Contour List "+contourList);
// Creating a copy of the image
//Mat copyOfImage = source;
Mat copyOfImage = newImageFortest;
logger.info("Copy of Image "+copyOfImage);
// Rectangle for cropping
Rect rectCrop = new Rect();
logger.info("Rectangle Crop New Object "+rectCrop);
// loop through the identified contours and crop them from the image to feed
// into Tesseract-OCR
for (int i = 0; i < contourList.size(); i++) {
// getting bound rectangle
rectCrop = Imgproc.boundingRect(contourList.get(i));
logger.info("Rectangle cropped"+rectCrop);
// cropping Image
Mat croppedImage = copyOfImage.submat(rectCrop.y, rectCrop.y + rectCrop.height, rectCrop.x,
rectCrop.x + rectCrop.width);
// writing cropped image to disk
logger.info("Path to write cropped image "+ tempDirectoryPath);
String writePath = tempDirectoryPath + "/croppedImg.png";
logger.info("writepath"+writePath);
// imagepath = imagepath.
Imgcodecs.imwrite(writePath, croppedImage);
try {
// extracting text from cropped image, goes to the image, extracts text and adds
// them to stringBuffer
logger.info("Exact Path where Image was written with Name "+ writePath);
String textExtracted = (tesseract
.doOCR(new File(writePath)));
//Adding Seperator
textExtracted = textExtracted + "_SEPERATOR_";
logger.info("Text Extracted "+textExtracted);
textExtracted = textExtracted + "\n";
text = textExtracted + text;
logger.info("Text Extracted Completely"+text);
// System.out.println("Andar Ka Text => " + text.toString());
} catch (Exception exception) {
logger.error(exception);
}
writePath = "";
logger.info("Making write Path empty for next Image "+ writePath);
}
}
catch(CvException ae)
{
logger.error("cv",ae);
}
catch(UnsatisfiedLinkError ae)
{
logger.error("unsatdif",ae);
}
catch(Exception ae)
{
logger.error("general",ae);
}
// converting into string
return text.toUpperCase();
}
// convert Mat to Image for GUI output
public static Image toBufferedImage(Mat m) {
// getting BYTE_GRAY formed image
int type = BufferedImage.TYPE_BYTE_GRAY;
if (m.channels() > 1) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels() * m.cols() * m.rows();
byte[] b = new byte[bufferSize];
m.get(0, 0, b); // get all the pixels
// creating buffered Image
BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
// returning Image
return image;
}
// method to display Mat format images using the GUI
private static void showWaitDestroy(String winname, Mat img) {
HighGui.imshow(winname, img);
HighGui.moveWindow(winname, 500, 0);
HighGui.waitKey(0);
HighGui.destroyWindow(winname);
}
}

Determine whether an image is grayscale in Java

I'm using something along the lines of this to do a (naive, apparently) check for the color space of JPEG images:
import java.io.*;
import java.awt.color.*;
import java.awt.image.*;
import javax.imageio.*;
class Test
{
public static void main(String[] args) throws java.lang.Exception
{
File f = new File(args[0]);
if (f.exists())
{
BufferedImage bi = ImageIO.read(f);
ColorSpace cs = bi.getColorModel().getColorSpace();
boolean isGrayscale = cs.getType() == ColorSpace.TYPE_GRAY;
System.out.println(isGrayscale);
}
}
}
Unfortunately this reports false for images that (visually) appear gray-only.
What check would do the right thing?
You can use this code:
File input = new File("inputImage.jpg");
BufferedImage image = ImageIO.read(input);
Raster ras = image.getRaster();
int elem = ras.getNumDataElements();
System.out.println("Number of Elems: " + elem);
If the number of elems returns 1, then its a greyscale image. If it returns 3, then its a color image.
the image looks like gray beacuse the r=g=b but actually it is a full color image, it has three channel r g b and the real gray image only have one channel

Incorrect number of Contours on javacv?

I have write simple code to retrieve number of Contours in a image and get the number of Contours in the image. But it always gives incorrect answer. Please can some one explain about this ?
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import java.io.File;
import javax.swing.JFileChooser;
public class TestBeam {
public static void main(String[] args) {
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage grayImage = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, 1);
cvCvtColor(src, grayImage, CV_RGB2GRAY);
cvThreshold(grayImage, grayImage, 127, 255, CV_THRESH_BINARY);
CvSeq cvSeq=new CvSeq();
CvMemStorage memory=CvMemStorage.create();
cvFindContours(grayImage, memory, cvSeq, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
System.out.println(cvSeq.elem_size());
CanvasFrame cnvs=new CanvasFrame("Beam");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
cnvs.showImage(src);
//cvShowImage("Final ", src);
}
}
This is the sample image that I used
But Code always returns output as 8. Please can someone explain this?
cvSeq.elem_size() will return size of sequence element in bytes and not the number of contours. That is why output is 8 every time.
Please refer following link for more information.
http://opencv.willowgarage.com/documentation/dynamic_structures.html#cvseq
For finding number of contours you can use following snippet
int i = 0;
while(cvSeq != null){
i = i + 1;
cvSeq = cvSeq.h_next();
}
System.out.println(i);
With the parameters you have provided CV_RETR_EXTERNAL will only provide external contour that is image boundary in your image(provided you are not inverting the image). You can use CV_RETR_LIST to get all the contours. Visit following link for more information on the parameters.
http://opencv.willowgarage.com/documentation/structural_analysis_and_shape_descriptors.html#findcontours

OpenCv Image conversion

When trying to do the watershed method i got this error:Unsupported format or combination of formats (Only 32-bit, 1-channel output images are supported) in cvWatershed
I think this is because my markers has 3 channels and a depth of 8. I think i need to convert this 3channel 8 depth image to a 32 bit 1 channel image. My question is am i right? and how do i do this conversion?
EDIT: Updated code with solutions
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kitouch;
import org.OpenNI.*;
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_calib3d.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import java.nio.ShortBuffer;
import java.awt.*;
import java.awt.image.*;
/**
*
* #author olivierjanssens
*/
public class watherShedExample {
// CanvasFrame frame5 = new CanvasFrame("Some T");
public static void main(String s[])
{
CanvasFrame frame1 = new CanvasFrame("Foreground");
CanvasFrame frame2 = new CanvasFrame("Dilated");
CanvasFrame frame3 = new CanvasFrame("Background");
CanvasFrame frame4 = new CanvasFrame("Markers");
CanvasFrame frame5 = new CanvasFrame("Watershed");
// Read input image
IplImage image = cvLoadImage("/Users/olivierjanssens/Downloads/images/group.jpg");
IplImage test = cvLoadImage("/Users/olivierjanssens/Downloads/images/binary.bmp");
IplImage binary = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
cvCvtColor(test, binary, CV_BGR2GRAY);
// Eliminate noise and smaller objects, repeat erosion 6 times
IplImage fg = cvCreateImage(cvGetSize(binary), binary.depth(), binary.nChannels() /* channels */);
cvErode(binary, fg, null /* 3x3 square */ , 6 /* iterations */);
frame1.showImage(fg);
// Identify image pixels pixels objects
IplImage bg = cvCreateImage(cvGetSize(binary), binary.depth(), binary.nChannels() /* channels */);
cvDilate(binary, bg, null /* 3x3 square */ , 6 /* iterations */);
frame2.showImage(bg);
cvThreshold(bg, bg, 1 /* threshold */ , 128 /* max value */ , CV_THRESH_BINARY_INV);
frame3.showImage(bg);
// Create marker image
IplImage markers = cvCreateImage(cvGetSize(binary), IPL_DEPTH_8U, binary.nChannels() /* channels */);
cvAdd(fg, bg, markers, null);
frame4.showImage(markers);
/*
* TEST SOLUTION 1
IplImage gray = cvCreateImage(cvGetSize(markers), IPL_DEPTH_8U, 1);
cvCvtColor(markers, gray, CV_BGR2GRAY);
IplImage img32bit1chan = cvCreateImage(cvGetSize(gray), IPL_DEPTH_32F, 1);
double ve;
for (int i = 0; i < gray.width(); i++) // markers width
{
for (int j = 0; j < gray.height(); j++) // markers height
{
ve = cvGetReal2D((IplImage)gray, j, i);
cvSetReal2D((IplImage)img32bit1chan , i, j, ve);
}
}
*/
//SOLUTION 2
IplImage markers32f = cvCreateImage(cvGetSize(binary), IPL_DEPTH_32F, binary.nChannels());
cvConvertScale(markers, markers32f, 1, 0); // converts from IPL_DEPTH_8U to IPL_DEPTH_32F
cvWatershed(image, markers32f);
frame5.showImage(image);
}
}
A manual conversion would look something like the following (haven't tested the code):
IplImage gray = cvCreateImage(cvGetSize(markers), IPL_DEPTH_8U, 1);
cvCvtColor(markers, gray, CV_BGR2GRAY);
IplImage img32bit1chan = cvCreateImage(cvGetSize(gray), IPL_DEPTH_32S, 1);
// convert 8-bit 1-channel image to 32-bit 1-channel
cvConvertScale(gray, img32bit1chan , 1/255.);
cvWatershed(image, img32bit1chan);
I'm not familiar with the Java API, but here is how you might go about it:
IplImage image = cvLoadImage("/Users/olivierjanssens/Downloads/images/group.jpg");
// this will force the image to be read as grayscale (i.e. single channel)
// sometimes saving a "binary" bitmap will result in a 3 channel image
IplImage binary = cvLoadImage("/Users/olivierjanssens/Downloads/images/binary.bmp", 0);
...
IplImage markers = cvCreateImage(cvGetSize(binary), IPL_DEPTH_8U, binary.nChannels() /* channels */);
cvAdd(fg, bg, markers, null);
frame4.showImage(markers);
IplImage markers32f = cvCreateImage(cvGetSize(binary), IPL_DEPTH_32F, binary.nChannels());
cvConvertScale(markers, markers32f, 1, 0); // converts from IPL_DEPTH_8U to IPL_DEPTH_32F
cvWatershed(image, markers32f);
See if that works for you.

PNG - Is it possible to reduce the palette using Java 2D?

If I have a PNG image opened as a BufferedImage, is it possible to reduce the palette in the PNG image so that there is less colour (less bits per pixel / colour depth)?
For example, if you look at Colour depth in Wikipedia, I would like to use 16 colours in my PNG image (3rd image down the right hand side).
If it's not possible with Java 2D, is there a library out there that will allow me to do this effectively?
I think Martijn Courteaux was right:
Here is example implementation:
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImagingTest2 {
public static void main(String[] args) throws IOException {
BufferedImage src = ImageIO.read(new File("in.png")); // 71 kb
// here goes custom palette
IndexColorModel cm = new IndexColorModel(
3, // 3 bits can store up to 8 colors
6, // here I use only 6
// RED GREEN1 GREEN2 BLUE WHITE BLACK
new byte[]{-100, 0, 0, 0, -1, 0},
new byte[]{ 0, -100, 60, 0, -1, 0},
new byte[]{ 0, 0, 0, -100, -1, 0});
// draw source image on new one, with custom palette
BufferedImage img = new BufferedImage(
src.getWidth(), src.getHeight(), // match source
BufferedImage.TYPE_BYTE_INDEXED, // required to work
cm); // custom color model (i.e. palette)
Graphics2D g2 = img.createGraphics();
g2.drawImage(src, 0, 0, null);
g2.dispose();
// output
ImageIO.write(img, "png", new File("out.png")); // 2,5 kb
}
}
Create a new BufferedImage with the lower palette and use createGraphic() to acquire a Graphics2D object. Draw the original image on the graphics. dispose() the graphics and here you are.
BufferedImage img = new BufferedImage(orig.getWidth(), orig.getHeight(),
BufferedImage.TYPE_USHORT_555_RGB);

Categories

Resources