Ask Your Question
0

Mat Syntax

asked Apr 10 '15

zms gravatar image

updated Apr 10 '15

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?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Apr 10 '15

berak gravatar image

updated Apr 10 '15

// 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.
Preview: (hide)

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 (Apr 10 '15)edit

is it about the path ? please see edit above .

berak gravatar imageberak (Apr 10 '15)edit

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

zms gravatar imagezms (Apr 10 '15)edit

no. the other way round, if you load it

berak gravatar imageberak (Apr 10 '15)edit

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

zms gravatar imagezms (Apr 10 '15)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 (Apr 14 '15)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 (Apr 14 '15)edit

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

berak gravatar imageberak (Apr 14 '15)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 (Apr 14 '15)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 (Apr 14 '15)edit

Question Tools

1 follower

Stats

Asked: Apr 10 '15

Seen: 747 times

Last updated: Apr 10 '15