a7ul / ImShow-Java-OpenCV

an alternative to imshow() in C++ OpenCV for Java OpenCV

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Closing window does not end process

sutr90 opened this issue · comments

When you display the window with image and then click the X button the window disappear but the Swing thread is still running in background. That is not desirable outcome. To fix this I would suggest adding following code:

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

For more informations on this topic see this thread:
http://stackoverflow.com/questions/7799940/jframe-exit-on-close-java

commented

Hey, Can it be used to display image in an android phone?

commented

Not sure. I don't have the android sdk. Probably not, you'likely want an
image view.

On Wednesday, August 19, 2015, kejriwalnishant1990 notifications@github.com
wrote:

Hey, Can it be used to display image in an android phone?


Reply to this email directly or view it on GitHub
#1 (comment)
.

commented

http://www.mkyong.com/android/android-imageview-example/

You could fork and submit a pull request

On Wednesday, August 19, 2015, kejriwalnishant1990 notifications@github.com
wrote:

Hey, Can it be used to display image in an android phone?


Reply to this email directly or view it on GitHub
#1 (comment)
.

Hi @kejriwalnishant1990
So u can use this function to get an image from your opencv Mat

public BufferedImage toBufferedImage(Mat m) {
        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
        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);
        return image;

    }

and then use the result in the ImageView of android

commented

Here's the same function if you like this one instead:

/** * Converts the desired inputMat to a {@link BufferedImage} so we can
see it * visually * * @param inputMat * the Matrix to convert to a {@link
BufferedImage} * @return a {@link BufferedImage} with the contents of the
matrix */ public static BufferedImage toBufferedImage(Mat inputMat) {
BufferedImage bi; int height = inputMat.rows(); int width = inputMat.cols();
byte[] imgData = new byte[(int) (inputMat.cols() * inputMat.rows() *
inputMat .elemSize())]; int type = BufferedImage.TYPE_BYTE_GRAY; if
(inputMat.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; } // Copy
the data into the matrix inputMat.get(0, 0, imgData); bi = new
BufferedImage(width,
height, type); bi.getRaster().setDataElements(0, 0, width, height, imgData);
return bi; }

Here's from a buffered image to a matrix:

/** * Creates a {@link Mat} which represents the given BufferedImage * *
@param bufferedImage * @return */ public static Mat toMatrix(BufferedImage
bufferedImage) { int imageType = bufferedImage.getType(); System.out.
println("ImageType:" + imageType); int matrixType = CvType.CV_8UC3; if
(imageType == BufferedImage.TYPE_BYTE_GRAY) matrixType = CvType.CV_8UC1;
byte[] pixels = ((DataBufferByte) bufferedImage.getRaster()
.getDataBuffer()).getData(); Mat matrix = new Mat(new Size(bufferedImage.
getWidth(), bufferedImage.getHeight()), matrixType); matrix.put(0, 0,
pixels); return MatHelper.fromBGR2RGB(matrix); } 

Here you can find some
matrix helper stuff:
https://github.com/AnEmortalKid/ImgProcPlayground/blob/stable/src/main/java/com/anemortalkid/imgutils/MatHelper.java

As always, you're welcome to fork the repository and create a pull request
if you'd like to share your ImShow.showImageView function.

On Wed, Aug 19, 2015 at 9:58 AM, Atul notifications@github.com wrote:

Hi @kejriwalnishant1990 https://github.com/kejriwalnishant1990
So u can use this function to get an image from your opencv Mat

public BufferedImage toBufferedImage(Mat m) {
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
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);
return image;

}

and then use the result in the ImageView of android


Reply to this email directly or view it on GitHub
#1 (comment)
.

commented

I am not able to find java.awt.image.BufferedImage . Is there any other jar file which i need to add in the code?

commented

Are you importing it? It should be part of the standard library.

http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html

On Wednesday, August 19, 2015, kejriwalnishant1990 notifications@github.com
wrote:

I am not able to find java.awt.image.BufferedImage . Is there any other
jar file which i need to add in the code?


Reply to this email directly or view it on GitHub
#1 (comment)
.

commented

No it is not part of java library for android. I have read on the internet BufferedImage class is not available in android. Is there any alternative?

commented

Not sure about that. Maybe you'll have to write your own based on android's
opencv

On Wednesday, August 19, 2015, kejriwalnishant1990 notifications@github.com
wrote:

No it is not part of java library for android. I have read on the internet
BufferedImage class is not available in android. Is there any alternative?


Reply to this email directly or view it on GitHub
#1 (comment)
.