Ask your own question, for FREE!
Mathematics 14 Online
OpenStudy (raffle_snaffle):

matlab

OpenStudy (raffle_snaffle):

@ikram002p Do you know much about pixel shifting?

OpenStudy (ikram002p):

nah sorry :'( as i don't have the program.

OpenStudy (raffle_snaffle):

Okay no worries, thanks.

OpenStudy (raffle_snaffle):

@jim_thompson5910

OpenStudy (raffle_snaffle):

Should I prompt the user or not? I am not sure really how to set up the for loop without it.

OpenStudy (raffle_snaffle):

I am suppose to use a nested for loop @jim_thompson5910

OpenStudy (raffle_snaffle):

OpenStudy (raffle_snaffle):

I got the image to shift without the loop

OpenStudy (raffle_snaffle):

do I have the general idea? I got the prompts to work, however can't get the new image to display (simg)

jimthompson5910 (jim_thompson5910):

why are you translating pixel by pixel? the function `imtranslate` shifts EVERY pixel of the image by the same amount http://www.mathworks.com/help/images/ref/imtranslate.html?searchHighlight=imtranslate

jimthompson5910 (jim_thompson5910):

I'm guessing your teacher wants you to do every pixel?

jimthompson5910 (jim_thompson5910):

oh I see now, the prof wants you to use nested loops

OpenStudy (raffle_snaffle):

yes and I am suppose to shift pixel by pixel?

OpenStudy (raffle_snaffle):

working on a small if else statement for original image.

jimthompson5910 (jim_thompson5910):

let me think

OpenStudy (raffle_snaffle):

clear, clc, close all; % clears all windows % loads image provided by instructor into matlab img = imread('dostoevsky.tif'); % x increases from 0 to 450, while % y decreses from 600 to 0 from bottom to top % displays image % asking use if wants to see original image original = input('Do you want to see original image? ', 's'); if (original == 'y' || original == 'Y') figure(1); imshow(img); axis on; elseif (original == 'n' || original == 'N'); fprintf('You said no. \n'); end % asking user to input x and y to transverse image x = input('Enter a X value, (must be a positive number): '); y = input('Enter a Y value, (must be a positive number): '); % for loops for k = 1:100 if (x > 0) % shifts image in x-positive direction simg(k) = imtranslate(img, [x, 0], 'outputview', 'full'); elseif (x < 0) fprintf('nothing will happen.'); end % second for loop for i = 1:100 if (y > 0) % shifts image y-positive direction simg(i) = imtranslate(img, [0, y], 'outputview', 'full'); elseif (y < 0) fprintf('nothing will happen.'); imshow(simg) end end end

jimthompson5910 (jim_thompson5910):

ok trying something out

OpenStudy (raffle_snaffle):

okay

jimthompson5910 (jim_thompson5910):

hmm I'm getting weird results, one moment

OpenStudy (raffle_snaffle):

okay. This is what I get Do you want to see original image? y Enter a X value, (must be a positive number): 100 Enter a Y value, (must be a positive number): 200 Subscripted assignment dimension mismatch.

jimthompson5910 (jim_thompson5910):

I think I got it, but I have this weird blue/green portion

OpenStudy (raffle_snaffle):

okay lol

OpenStudy (raffle_snaffle):

what do you mean by blue/green portion?

jimthompson5910 (jim_thompson5910):

ok what I did was hard code x = 50 and y = 100 just so I didn't have to type in x and y each time I tested this script

jimthompson5910 (jim_thompson5910):

here's the m file and what the figure window looks like (2 attachments)

OpenStudy (raffle_snaffle):

reading it now

OpenStudy (raffle_snaffle):

% get the size of matrix img [m,n] = size(img); % set up matrix of numbering but 0s % this blank matrix is set to the size of matrix img simg = uint8(zeros(size(img))); % this matrix will later be filled by % the values found in matrix img

OpenStudy (raffle_snaffle):

What does the top command do?

jimthompson5910 (jim_thompson5910):

jimthompson5910 (jim_thompson5910):

this command `[m,n] = size(img);` ?

OpenStudy (raffle_snaffle):

yeah and don't worry about the color.

OpenStudy (raffle_snaffle):

Does that command just get the size of the matrix?

OpenStudy (raffle_snaffle):

simg = uint8(zeros(size(img))); and then this command sets a zero matrix up

jimthompson5910 (jim_thompson5910):

`Does that command just get the size of the matrix?` yes the command "[m,n] = size(img);" gets the size of the matrix m rows n columns

OpenStudy (raffle_snaffle):

and then what about simg = uint8(zeros(size(img)));

jimthompson5910 (jim_thompson5910):

sets up a matrix of zeros it's the same size as matrix img

OpenStudy (raffle_snaffle):

okay let me read on some more

OpenStudy (raffle_snaffle):

What about this bit of code? for j = 1:1:m % 'for' loop designed to go through each column for k = 1:1:n % check to see if in bounds if( (j+y <= m) && (k+x <= n) ) % this line of code will assign the pixel value % of matrix img to matrix simg % with the shifts in mind simg(j+y,k+x) = img(j,k);

OpenStudy (raffle_snaffle):

why did you go j = 1:1:m and k = 1:1:n

OpenStudy (raffle_snaffle):

so j+y has to less or equal to m, m being the rows and k+x has to be less or equal to n because n is the columns

OpenStudy (raffle_snaffle):

basically we can't go beyond the size of the matrix both in row and column

jimthompson5910 (jim_thompson5910):

one question at a time please

jimthompson5910 (jim_thompson5910):

so you figured out the question when you asked "why did you go j = 1:1:m and k = 1:1:n" ?

OpenStudy (raffle_snaffle):

sorry I will ask you one question at a time Not really to the last question so you figured out the question when you asked "why did you go j = 1:1:m and k = 1:1:n" ?

jimthompson5910 (jim_thompson5910):

j starts at 1, increments by 1, and keeps increment til it gets to m m = number of rows

jimthompson5910 (jim_thompson5910):

k starts at 1, increments by 1, stops increasing til it equals n n = number of columns

jimthompson5910 (jim_thompson5910):

j and k are indices to help go through the 2 'for' loops

OpenStudy (raffle_snaffle):

okay

OpenStudy (raffle_snaffle):

What about this part? % check to see if in bounds if( (j+y <= m) && (k+x <= n) ) % this line of code will assign the pixel value % of matrix img to matrix simg % with the shifts in mind simg(j+y,k+x) = img(j,k);

jimthompson5910 (jim_thompson5910):

`if( (j+y <= m) && (k+x <= n) ) ` is to make sure that I didn't go past the boundaries I don't want to write in values into the simg matrix that shouldn't be there

jimthompson5910 (jim_thompson5910):

`simg(j+y,k+x) = img(j,k);` is the part of code that does the actual shifting though for some reason, the blue part is there and I still don't know why

OpenStudy (raffle_snaffle):

okay makes a lot of sense. I thought that was the case but just wanted to make sure my logic was correct.

OpenStudy (raffle_snaffle):

so img is the original and simg is the new image with the shift constraints?

jimthompson5910 (jim_thompson5910):

yes img = original simg = shifted

jimthompson5910 (jim_thompson5910):

I guess img = image simg = shifted+image

OpenStudy (raffle_snaffle):

okay that makes more sense.

OpenStudy (raffle_snaffle):

uint8 ^^what does this command do

OpenStudy (raffle_snaffle):

simg = uint8(zeros(size(img))); ^^^^ This

jimthompson5910 (jim_thompson5910):

it converts the values to uint8 data types I think

jimthompson5910 (jim_thompson5910):

uint = unsigned integer uint8 = 8 bit unsigned integer just a guess really

OpenStudy (raffle_snaffle):

How did you know that we were not to exceed the mxn dimensions?

OpenStudy (raffle_snaffle):

I knew the for loop needed a specific constraint but didn't know what it was suppose to be.

jimthompson5910 (jim_thompson5910):

so the image shifts properly. Otherwise, the simg would be bigger than img

OpenStudy (raffle_snaffle):

when you say 'bigger' you mean the size of the matrix?

jimthompson5910 (jim_thompson5910):

IF I didn't do those bounds checks, then simg would be bigger let's say x = 50 y = 100 m = 200 n = 300 img would be a 200x300 matrix simg would be a 250x400 matrix that is if I didn't check to see if you went out of bounds or not

OpenStudy (raffle_snaffle):

okay okay I see what you are saying.

OpenStudy (raffle_snaffle):

Because if we didn't have the bounds there is a possibility the image would be 'hidden'. It would have been displayed somewhere not visible to the user?

jimthompson5910 (jim_thompson5910):

|dw:1464663175284:dw|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!