image flipping without built in commands

조회 수: 2 (최근 30일)
Johann Sebastian Bach
Johann Sebastian Bach 2016년 7월 22일
댓글: Johann Sebastian Bach 2016년 7월 23일
Hey everyone, I am trying to horizontally flip an image using arrays only. What I tried to do with this code was to go through every row in the original image and assign the values of every pixel in a row of the original image to a pixel in the processed image with the same row but reversed order. (meaning the value of the pixel in the first row the first column of the original image would be assigned to the pixel in the first row the last column of the processed image)
original_img=imread('someimg.jpg');
[row,col,layers] = size(original_img);
processed_img = zeros(row,col,layers);
processed_img(1:row,col:-1:1,:) = original_img(1:row,1:col,:);
The problem is that the algorithm seems to work for some pixels in the image but most of the processed image is still white (since it was initially an array of zeros). I couldn't get my head around the reason why this algorithm does not work. I know there are built-in functions to flip images but I am trying to do it by using array properties. Thanks a lot for your help.

채택된 답변

Image Analyst
Image Analyst 2016년 7월 23일
The reason it doesn't work is that processed_img is a double instead of a uint8. imshow() is meant for uint8 unless you use the [] option.
imshow(processed_img, []);
So make it a uint8 by initializing it like this:
processed_img = zeros(row, col, layers, 'uint8');
or else just get rid of that line entirely since it's not needed at all and just slows things down.
  댓글 수: 1
Johann Sebastian Bach
Johann Sebastian Bach 2016년 7월 23일
Thank you, it works now. I have not thought about this.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

James Tursa
James Tursa 2016년 7월 22일
Did you mean this?
newcat(1:row,1:col,:) = cat(row:-1:1,1:col,:);
Or this?
newcat(1:row,1:col,:) = cat(1:row,col:-1:1,:);
  댓글 수: 3
James Tursa
James Tursa 2016년 7월 22일
Can you elaborate on "doesn't work?" What result are you expecting?
Johann Sebastian Bach
Johann Sebastian Bach 2016년 7월 23일
Thanks a lot first of all James for your help. I didn't make myself clear the first time, I changed my post so that the main question is understandable. Thanks a lot.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 7월 23일
You should never use variables that are also built in functions. And you not only did it once, but twice. Don't use cat and dim as variables because those are built in functions.
What do you mean by "flip"? Flip vertically or horizontally or both? And why can't you use built-in functions? Are you asking us to do your homework for you?
  댓글 수: 1
Johann Sebastian Bach
Johann Sebastian Bach 2016년 7월 23일
편집: Johann Sebastian Bach 2016년 7월 23일
Thank you for your kind response Image Analyst. My question is "Why does the code I have written not work in the way it should?" and is not "Would you do my homework for me ?". Since Matlab re-assigns the values of the variables used, I don't think the main problem with my code is that I used built-in function names as variables. Yes I mean a horizontal flip. What I tried to do with my algorithm was that I created an array of zeros with the same dimensions as an RGB image and reversed the order of pixels in each row in the original image , cat, and substituted them into the processed image newcat. (If you are curious, the reason I called the variable names cat and newcat was because the image I have been asked to flip horizontally was a jpg image of a kitten.) Thanks a lot for your concern.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by