Ask Your Question
0

Mat Syntax

asked 2015-04-10 05:36:52 -0600

zms gravatar image

updated 2015-04-10 05:54:03 -0600

Hi, I'm doing the tutorial for ANN. One of the instruction is to do this

Generate cv::Mat data(1,ATTRIBUTES,CV_32S) which will contain the pixel

OK, I have the image 16X16 binary already but I don't know how to do the above this because i'm using the imread syntax like this.

image = imread("c://DL.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file

it is not the same.. Can anyone help me on this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-04-10 06:01:43 -0600

berak gravatar image

updated 2015-04-10 06:22:26 -0600

// step 1: load the image as grayscale (not color):
image = imread("c:/DL.jpg", CV_LOAD_IMAGE_GRAYSCALE); // 1 forward slash, or 2 backward ones, please.

// step 2: flatten it to a 1d array:
image = image.reshape(1,1);

// step 3, convert the data to something the ann likes, i'd even prefer float, not int there:
img.convertTo(image, CV_32F); // or CV32S, if you really want int.
edit flag offensive delete link more

Comments

Hi Berak, Tq for the answer, just another question because I hv the binary image.. Would it be different then as per syntax below?

image = imread("c://DL.jpg", CV_LOAD_IMAGE_GRAYSCALE);

zms gravatar imagezms ( 2015-04-10 06:14:06 -0600 )edit

is it about the path ? please see edit above .

berak gravatar imageberak ( 2015-04-10 06:23:00 -0600 )edit

no, it is about this.. CV_LOAD_IMAGE_GRAYSCALE but my image is binary.. would it be a problem?

zms gravatar imagezms ( 2015-04-10 06:26:57 -0600 )edit

no. the other way round, if you load it

berak gravatar imageberak ( 2015-04-10 06:29:15 -0600 )edit

ok, let me try first and will inform.. TQ

zms gravatar imagezms ( 2015-04-10 06:31:11 -0600 )edit

Hi Berak, I'm sorry to ask again but appreciate so much on yr answer. I ran the code and list the value of the Mat. It is graylevel and not binary anymore. Am I wrong? image = [0, 255, 1, 0, 0, 1, 0, 2, 0, 3, 0, 0, 0, 1, 0, 1; 255, 0, 251, 3, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 5, 0; 255, 192, 2, 255, 0, 0, 3, 0, 2, 1, 1, 0, 0, 1, 0, 2; 0, 254, 252, 88, 255, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2; 1, 25, 252, 104, 69, 254, 1, 0, 1, 1, 2, 0, 1, 0, 0, 0; 0, 0, 193, 253, 0, 191, 255, 1, 0, 0, 0, 4, 0, 0, 2, 0; 0, 2, 3, 250, 255, 1, 252, 255, 1, 1, 0, 0, 0, 1, 3, 0; 0 ...(more)

zms gravatar imagezms ( 2015-04-14 01:38:31 -0600 )edit

so, seems like jpg compression messed with your img. (avoid jpg with computer-vision!)

do you really need a binary image ? then apply a threshold() or similar

berak gravatar imageberak ( 2015-04-14 01:55:30 -0600 )edit

png, bmp,pgm, anything 'non-lossy' (or uncompressed)

berak gravatar imageberak ( 2015-04-14 02:12:47 -0600 )edit

Hi Berak, I had apply the thresholding to get the binary but, when I load it back, it is in greyscale format. here is my code. I had checked one by one, is binary code is not able to be printed out?

Mat frame = imread("C:\\NN\\DebugClass\\Image91.png", CV_LOAD_IMAGE_COLOR); 
Rect rectangle (0,0,frame.cols,frame.rows/2);
    frame = frame(rectangle);
    bitwise_and(frame,mask,frame);  
    imshow("aftermask",frame);

Rect myROI(925, 197, 233, 104);
int i;
int a=0;
Size size(16,16);//the dst image size,e.g.100x100

frame = frame(myROI);
cv::resize(frame,frame,size);//resize image
imshow("scaledown",frame);

Mat data(1,ATTRIBUTES,CV_32S);
    Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
threshold( frame_gray, frame_gray, 220, 255,THRESH_BINARY);
zms gravatar imagezms ( 2015-04-14 05:06:19 -0600 )edit

Hi berak, I found that the binary 1 and 0 will not be ok as in this article. http://docs.opencv.org/doc/tutorials/...

Anyway, I can convert to string the 0 and 1for binary from 1 to 255 and I need to save it as cv::Mat data(1,ATTRIBUTES,CV_32S).

Do u know how to do it?

Thanks

zms gravatar imagezms ( 2015-04-14 06:01:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-10 05:36:52 -0600

Seen: 722 times

Last updated: Apr 10 '15