Ask Your Question

capture in greyscale

asked 2014-11-04 09:52:29 -0500

lobsterman gravatar image

hi all, i am trying to get every ounce of speed from my opencv/java videocapture. System works like a charm, but i think i am still wasting clock cycles ...

Currently i read color frames from videocapture device into Mat, update the content of a BufferedImage via arraycopy, then repaint into jpanel. at 1280x720 i get around 20fps with fairly old Mac Powerbook hardware, which is not bad. In my application, grayscale is actually good enough. i know how to change the color frame to a grayscale one using

Imgproc.cvtColor(MatImg, MatImg_mod, Imgproc.COLOR_RGB2GRAY);

but that gains me nothing for speed. Anyone know of a way to already pull frames off the camera in grayscale? or to do the Y channel only in the YUV route? any suggestions for how to squeeze some speed out of this puppy and any code examples greatly appreciated. thanks,

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-11-04 12:45:11 -0500

Doombot gravatar image

Well, is your camera RGB (probably by USB or Ethernet) or is it a video camera already in YUV and you use a frame grabber (adapter card) to get the images in the computer?

You want to use grayscale for some reasons (I do use grayscale too), but you must understand that if your camera feeds you with a RGB image or even more strictly a NOT-Grayscale format, something somewhere has to convert it to grayscale. There are various ways to do it (, but it takes computation time. So maybe you won't really save time by only displaying an image in grayscale instead of it's native color format.

Where you might get improved performance is if you perform computations on the frames. A RGB image has three channels whereas a grayscale has only one, three time less pixels to works on. Don't hesitate if you want me to elaborate on this...

About YUV, I don't know if there is a option in Java to capture directly in that format (even if you happen to have a YUV video feed, the adapter card will probably convert it to RGB or BGR). If not, you need to convert it to this colorspace so again it is computation time.

Maybe there is an issue somewhere else in your code that slows thing, though. I would advise to post some of your code in an edit to your question. I am myself not knowledgeable in Java but surely someone here is!

edit flag offensive delete link more


thanks a stack for the answer. right now i am still learning the basic opencv framework and i am using the build-in facetime HD camera for that, not sure whether this one offers yuv. the camera i will try to interface for the real need for speed in grayscale processing is the elphel open source design type camera (elphel 353) <>. the elphel does seem to offer a bunch of formats but i am not sure how to make opencv talk to it with the right settings, i know i can pull out rgb and convert, but if there is any way to specifically pull out the Y channel of YUV, or if i can just read frames in grayscale, then this puppy would be seriously cooking. asynchronous access would be sweet too. for code i get the frames as Mat with

lobsterman gravatar imagelobsterman ( 2014-11-04 16:02:11 -0500 )edit

Part 1: Personally, I don't know the specifics of how a specific camera is interfaced with OpenCV, beside the fact that it has to do with the camera driver, the OS and the OpenCV library. What I know is that usually, if a camera is plugged by USB or ethernet, it provides either a RGB (3 channels) frame, a greyscale (1 channel) or even black and white (1 channel). This is done by the hardware of the camera, and if you don't know from the info provided on the website, be safe and ask the manufacturer. About YCrCb (we mistakenly named it YUV, see this ), well a couple years ago I was working with a "TV camera", you know one with a "coax" plug so it was pluggable on a regular TV. We had to use this kind of camera because of ...(more)

Doombot gravatar imageDoombot ( 2014-11-05 07:42:07 -0500 )edit

Part 2: So my boss bought an adapter card that took the "coax" signal and allowed the stream to be fed by USB to the computer. Then, when I captured a frame, it was a three channel Y Cr Cb so in this case, I could have simply read only the first channel to get only the "Y" information. Ironically, was I had to do then was... convert the YCrCb frame into RGB. The bottom line is that if you really only need a greyscale image and that your computer isn't fast enough at converting RGB to greyscale, well you should have a camera that provides it directly. That's the best way to ensure speed. Note 1: Right now, on a Intel Core i7 I can afford to use a RGB camera and convert the images to greyscale on the fly, so it is more versatile for dev ...(more)

Doombot gravatar imageDoombot ( 2014-11-05 07:50:16 -0500 )edit

Note 2: In the previous job, we were not using OpenCV, for some reason it was LabVIEW (don't ask why... ;) )

Doombot gravatar imageDoombot ( 2014-11-05 07:51:35 -0500 )edit

thanks, doombot, yeah, in my app grayscale is sufficient and i am getting 22fps at 1280x720 which is quite ok, but i would like to progress towards the camera's max of 60fps in asynchronous mode. for that my ability will yet need to improve towards reading frames in grayscale directly as CV_8U1 rather than to capture in 3 channels in CV_8U3 and reducing it in a later step. it would be greatly appreciated if anyone has any insights on how opencv specifically interfaces with the camera and what of that functionality is accessible to me in java.

lobsterman gravatar imagelobsterman ( 2014-11-08 17:36:35 -0500 )edit

Rapidly, it is really possible that this specific model of camera may ONLY provide RGB frames, harware-ly speaking. IF this is the case (ask the manufacturer), then, considering that you plug it directly in the computer with no hardware adapter card, any format conversion has to be done by software. It might be the driver, it might be the OpenCV lib, it might be you "by hand" in your program, but in the end it might eats CPU time. What I would advise, in order: check what are the possible output format of your camera. Then, maybe ask a new question asking specifically how to access images in that format for that specific camera. There is more chance that someone look at a new question than an older question IMHO. This is sadly the end of my knowledge. Good luck!

Doombot gravatar imageDoombot ( 2014-11-10 08:48:59 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2014-11-04 09:52:29 -0500

Seen: 2,516 times

Last updated: Nov 04 '14