MATLAB Answers

# Moving a greyscale image such that the lowest pixel with an intensity above 0 is at the bottom of the image.

11 views (last 30 days)
Cyrus N on 28 Jun 2017
Answered: Image Analyst on 29 Jun 2017
I'm trying to translate a greyscale image such that the bottommost pixel with an intensity>0 is at the bottom of the pic, I already have it such that the left most pixel . My code for translating is as follows: lungc_ref = imref2d(size(c)) [row, column] = find(c, 1, 'first'); [row2 column2] = find(c, 1, 'last'); t = [1 0 0; 0 1 0; -(column-1) (256-row2) 1]; tform = affine2d(t); imwarp(c, tform); [c_translated,lungc_ref] = imwarp(c,tform,'OutputView',lungc_ref); imshow(c_translated, [0 1000])
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Massimo Zanetti on 29 Jun 2017
Edited: Massimo Zanetti on 29 Jun 2017
Use the circshift function, which shifts arrays along one dimension. Here is an example:
%generate an image (matrix)
A = rand(6,9)
%find linear index of the minimum value
[~,ix] = min(A(:));
%convert linear index into subscript index
[r,c] = ind2sub(size(A),ix)
%shift image rows to put the lowest valued pixel at the bottom
Y = circshift(A,size(A,1)-r,1)
%additionally, you can also shift image columns to put the lowest valued pixel at the left
Y = circshift(Y,size(A,2)-c+1,2)
Check it out.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### More Answers (2)

Cyrus N on 29 Jun 2017
Alternatively I found that lowestRow = find(sum(one, 2)>0, 1, 'last'); also seems to work for finding the bottom row after which you can subtract the value for lowestRow from the image dimension to affix the bottom row of the grey image to the bottom row of the figure.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Image Analyst on 29 Jun 2017
Simply figure out how much to move the image, then call imtranslate(). It's different than circshift in that it doesn't wrap the image around to the other side.
Description
B = imtranslate(A,translation) translates image A by the translation vector specified in translation. If A has more than two dimensions and translation is a two-element vector, imtranslate applies a 2-D translation to A, one plane at a time.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.69893575,"math_prob":0.9183271,"size":496,"snap":"2021-04-2021-17","text_gpt3_token_len":169,"char_repetition_ratio":0.12398374,"word_repetition_ratio":0.0,"special_character_ratio":0.35080644,"punctuation_ratio":0.20560747,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96624523,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-12T18:57:11Z\",\"WARC-Record-ID\":\"<urn:uuid:e86568eb-0312-48b5-8f0e-5fa62cc23bf4>\",\"Content-Length\":\"129016\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4666df4c-504c-499f-ab76-678722ee83ac>\",\"WARC-Concurrent-To\":\"<urn:uuid:442f41be-801f-41a5-b2f5-93ad9684606f>\",\"WARC-IP-Address\":\"23.197.108.134\",\"WARC-Target-URI\":\"https://ch.mathworks.com/matlabcentral/answers/346673-moving-a-greyscale-image-such-that-the-lowest-pixel-with-an-intensity-above-0-is-at-the-bottom-of-th\",\"WARC-Payload-Digest\":\"sha1:OZ3VOGLZDCRQCVTN5NCSDHL4JMX4VZZJ\",\"WARC-Block-Digest\":\"sha1:W6QRTIWYV4LJWJXQASIIM5HYSPLU2NOZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038069133.25_warc_CC-MAIN-20210412175257-20210412205257-00534.warc.gz\"}"}