Simple Imaging questions , dealing with matrix maniuplation

2 views (last 30 days)
Hi, I am a first year matlab student, and this is really confusing me!
create function: shrink(image) Makes the image half as large in both height and width. Each pixel should be the average of 4 pixels. Input image must have even rows/columns. You’re not allowed to use imresize.
and create another function: zoom(image) Makes the image twice as large in both height and width. Each pixel in the image is replicated to become 4 pixels. You’re not allowed to use imresize.
I dont even know where to start :( this course is killing me; I am an environmental science student, I didnt sign up for this :(
can someone please explain! I know you guys probably think its too simple and I should just KNOW this stuff , but I am just learningin and it doesnt make sense. ive been reading stupid internet articles that cant explain anything , ! i know i have to do some kind of matrix indexing crap like create a matrix of zeroes and input the numbers into it, but how do i go aboiutr s getting those numbers into the matrix. I did other questions on rotating , and flipping the images, rescaling brightness ; but this i just dont understand , i really need helpin the assignment , but this one is just really confusing me ; ihave spent like 12 houirs on this , know it is simple , buit i just cant think and I need a new perspective on this , plrease someone give me some direction
  1 Comment
David Young
David Young on 27 Feb 2011
It's very difficult to answer without just doing the problem for you - which wouldn't help in the end.
A good place to start might be to look at how images are indexed, and to look at the documentation on the : operator. Start with the "Matrices and Arrays" chapter of the getting started documentation, accessible from the help window.
It's extremely useful to understand computer programming if you're a science student of any sort. You'll be glad you did the course.

Sign in to comment.

Accepted Answer

Matt Tearle
Matt Tearle on 28 Feb 2011
OK, so I'm trying to walk the line between helping you and doing your homework... so, how about this? I'll step you through solving a very similar problem, but in 1 dimension. I'll let you figure out the 2-D version.
Problem: given a vector, such as x = [3,5,0,0,7,6,3,5,2,0], find the difference between the consecutive pairs. That is, [2,0,-1,2,-2].
Solution: The first element of the solution is x(2)-x(1); the 2nd element is x(4)-x(3); the 3rd is x(6)-x(5); the 4th is x(8)-x(7)... OK, so I see a pattern. solution(k) = x(2*k)-x(2*k-1). Hence, brute-force approach would be
y = zeros(1,5);
for k=1:5
y(k) = x(2*k) - x(2*k-1);
end
But a more "MATLAB" way to look at this would be to see that we're taking the even-numbered elements minus the odd-numbered ones. To get the even-numbered elements: x(2:2:end) This will return the vector [5,0,6,5,0]. Similarly, the odd-numbered elements are x(1:2:end), which gives [3,0,7,3,2]. So now we have two vectors of 5 points. Take the difference and we're done:
y = x(2:2:end) - x(1:2:end)
Now change difference into average, and extend to 2-D arrays, and your homework is done and dusted. I hope!
A final word of warning: make sure you're doing everything with doubles, not uint8. Images are often in uint8, but that will cause headaches when you try to do calculations (like add together and divide by 4, for example). If necessary: x = double(x).

More Answers (2)

Matt Tearle
Matt Tearle on 27 Feb 2011
Start by working out how to do it on paper/by hand for a very small example (2-by-4, etc). Work out the rule/formula for how the indices of the new image relate to the indices of the old image. Maybe even start in one dimension while you get the hang of MATLAB indexing.
The simple way will be to loop over the indices
for k=1:n
A(k) = some calculation
end
However, a simple solution exists, using MATLAB's powerful indexing constructs. Read up on the colon (range) operator. As a simple, related example, the diff function calculates the n-1 intervals between a vector of n points; equivalent syntax is
x(2:end) - x(1:end-1)
If you can wrap your head around that, you should be able to solve the hw problem.
  1 Comment
miskie
miskie on 27 Feb 2011
ugh, ive been trying to figure this out for hours now , !!
I know how to index matrices and stuff , but I dont know really where to start.
x=zeros(size(image)*2)
% creates empty matrix of double size
i=image(1:end, 1:end)*2
something like this ?? or
x=i

Sign in to comment.


miskie
miskie on 28 Feb 2011
Hi , thanks for the help! I think I am close to coming up with the solution , but it still doesn' t seem to work ; but this is what I got so far
function [pic]= shrink(image)
i=imread(image);
i=double(i);
p=zeros(size(i)/2);
w=i(:,(1:2:end));
h=i(:,(2:2:end));
j=w+h
x=j((1:2:end),:)+j((2:2:end),:);
x=(x/4);
x=uint8(x);
p=x;
pic=imshow(x);
but not work for an image , it seems to extend the width of the image not shrink it ; i dont know how it would do that since the size matrix was reduced by half
  2 Comments
Matt Tearle
Matt Tearle on 28 Feb 2011
Pretty much works for me... a couple of comments:
1) is "image" a filename or an array? you define "i" twice, once by reading from a file, then by converting the input to double. did you perhaps intend the 3rd line to be "i = double(i)"?
2) imshow is a bit tricky about how it messes with axes. you might want to keep things simple and use image and axis. if you do, make sure you change the name of the variable "image", otherwise you're going to run into some fun name clashes. and by fun i mean annoying.
3) having talked about 2-d images, i was skipping over the inconvenient truth that true-color images in MATLAB are actually 3-d arrays. however, everything you've done will work, you just need to do it all uniformly across the third dimension (which is color, btw). basically, anywhere you index into i(y,x), change it to i(y,x,:).
4) minor thing, but what does the penultimate line do? (p=x) similarly, the line p = zeros... doesn't do anything either.
miskie
miskie on 28 Feb 2011
OMG matt u rock!
"having talked about 2-d images, i was skipping over the inconvenient truth that true-color images in MATLAB are actually 3-d arrays. however, everything you've done will work, you just need to do it all uniformly across the third dimension (which is color, btw). basically, anywhere you index into i(y,x), change it to i(y,x,:). "
I was actually pulling out my hair over this and the answer was so simple! that is sooo awesome !!!!! yes yes yes yes yes!!! thanks for explaining that to me

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!