Go to Post No! I will not be an enabler for this madness. Go outside. See a movie. Read a book. LIVE!!!! - Herodotus [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 29-01-2015, 21:02
MatthewC529 MatthewC529 is offline
Lcom/mattc/halp;
AKA: Matthew
FRC #1554 (Oceanside Sailors)
Team Role: Mentor
 
Join Date: Feb 2014
Rookie Year: 2013
Location: New York
Posts: 39
MatthewC529 is on a distinguished road
Re: OpenCV USBCamera convert to Mat

I don't believe this is the typical way of converting a Mat to a BufferedImage. From my experience it is much similar to use MatOfByte:

Code:
// Lots of code...

Mat mat = getSomeImageMat();
MatOfByte bytes = new MatOfByte();

Highgui.imencode(someFormat, mat, bytes);

byte[] arr = bytes.toArray();
ByteArrayInputStream bis = new ByteArrayInputStream(arr);

BufferedImage img = ImageIO.read(bis);

// Even more code...
// Exceptions and Stream Closing omitted for brevity
This is what I have seen typically used. It's much simpler.

(I noticed you used this algorithm, but I will leave it here for future reference)
http://stackoverflow.com/questions/1...-mat-in-opencv

Is there some limitation where you can't use MatOfByte?
__________________
Team 1554 -- Oceanside Sailors
  • 2013-2014 - Lead/Sole Programmer
  • 2014-2015 - Lead Programmer, President
  • 2015-? - Team 1554 Mentor
Independent Public Projects

Developer at Legend Zero LLC.
Java/C++ Programmer

Last edited by MatthewC529 : 29-01-2015 at 21:25. Reason: Me Being Overzealous with Solutions :)
  #2   Spotlight this post!  
Unread 30-01-2015, 12:12
JML's Avatar
JML JML is offline
Jlyon
AKA: Jlyon
FRC #3044 (0xBe4)
Team Role: Programmer
 
Join Date: Oct 2012
Rookie Year: 2012
Location: 3044
Posts: 17
JML is an unknown quantity at this point
Re: OpenCV USBCamera convert to Mat

I attached a screenshot of how the images come out with my current method after converting from BufferedImage to Mat, im going to try with the MatOfBte Method and see if that works, thanks for the help
Attached Thumbnails
Click image for larger version

Name:	img.png
Views:	26
Size:	476.3 KB
ID:	18059  
  #3   Spotlight this post!  
Unread 30-01-2015, 12:28
JML's Avatar
JML JML is offline
Jlyon
AKA: Jlyon
FRC #3044 (0xBe4)
Team Role: Programmer
 
Join Date: Oct 2012
Rookie Year: 2012
Location: 3044
Posts: 17
JML is an unknown quantity at this point
Re: OpenCV USBCamera convert to Mat

Quote:
Originally Posted by MatthewC529 View Post

Is there some limitation where you can't use MatOfByte?
No, there is no limitation where we cant use MatOfByte, but i don't believe the issue lies in that part of the code, i can convert a Mat to a BufferedImage without any problems... the problem comes in when I try to take the image from the usb camera, which comes in as a BufferedImage, and convert it to a Mat.
  #4   Spotlight this post!  
Unread 30-01-2015, 14:59
JML's Avatar
JML JML is offline
Jlyon
AKA: Jlyon
FRC #3044 (0xBe4)
Team Role: Programmer
 
Join Date: Oct 2012
Rookie Year: 2012
Location: 3044
Posts: 17
JML is an unknown quantity at this point
Re: OpenCV USBCamera convert to Mat

For whatever reason, switching the Mat Declaration to the one shown seemed to fix the problem, for anyone who finds this in the future.
Code:
public static Mat bufferedImageToMat(BufferedImage i) {

	byte[] pixels = ((DataBufferByte) i.getRaster().getDataBuffer()).getData();
	Mat img = new Mat();
	img = new Mat(new Size(i.getWidth(), i.getHeight()), CvType.CV_8UC3);
	img.put(0, 0, pixels);

	return img;
}
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 01:38.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi