Ask Your Question
0

fastest way to access individual bytes in CV_8U1

asked 2014-11-08 17:49:15 -0500

lobsterman gravatar image

updated 2014-11-09 07:23:54 -0500

berak gravatar image

I am hoping to squeeze every ounce of speed from opencv videocapture. Can someone please enlighten me as to whether there is a better method to access individual pixel grayscale values as i iterate through a CV_8U1. i am using currently

public int getValue (int Col, int Row) { 
    return (int)itsMat.get(Row,Col)[0];
}

all help greatly appreciated.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-11-09 07:23:19 -0500

berak gravatar image

updated 2014-11-09 08:21:16 -0500

the fastest way is not accessing single bytes at all.

please use:

Mat bgr  = ...; // your input
Mat gray = new Mat();
Imgproc.cvtColor( bgr, gray, Imgproc.COLOR_BGR2GRAY);

http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html#cvtColor(org.opencv.core.Mat,%20org.opencv.core.Mat,%20int)

[edit]

if you ever must access bytes, at least put/get the whole batch:

byte[] bytes = new byte[ mat.total() * mat.elemSize() ];
mat.get(0,0, bytes);
// ...
mat.put(0,0, bytes);
edit flag offensive delete link more

Comments

i already have the grayscale image (CV_8U1), that is why i need to get the value of single bytes per pixel. the question is about what the fastest way is to get that pixel value.

lobsterman gravatar imagelobsterman ( 2014-11-09 08:05:21 -0500 )edit

^^ please see edit.

you never told us, what you're trying to achieve. there might be a builtin function for that already, too.

berak gravatar imageberak ( 2014-11-09 08:22:50 -0500 )edit
1

thanks a stack. sorry, put it into title that i needed to grab from 8U1 image. just tried this and grabbing to whole byte array and iterating through that really makes this fly. iterating through pixel by pixel with get takes 10ms for a hd frame, grabbing all in one bunch and then reading from bytes[] takes .... wait for it ... 0.1ms. thanks a major stack berak

lobsterman gravatar imagelobsterman ( 2014-11-09 08:32:55 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-11-08 17:49:15 -0500

Seen: 291 times

Last updated: Nov 09 '14