how do i crop an image using its matrix data?

3 ビュー (過去 30 日間)
vignesh
vignesh 2013 年 7 月 11 日
I have a jpeg image with the required image only in the center. i need to crop the white portion of the image i.e to obtain only the center image. how do i do this?
thanks.

採用された回答

Evan
Evan 2013 年 7 月 11 日
編集済み: Evan 2013 年 7 月 11 日
It's a bit hard to tell what exactly needs to be done without you defining your need in a more detailed way. Do you want to only plot a portion of the data and retain the rest? Do you want to throw out any row with a saturated (255) pixel in it? Do you only want to throw out rows where a certain percentage is saturated? Etc.
Here's a generalized answer, but you'll have to tailor it too what exactly you want:
outOfBounds = img >= maxBrightness;
croppedImg = img;
croppedImg(sum(outOfBounds,2) >= pctg * size(img,2),:) = [];
This, of course, if very general. You set maxBrightness yourself. It could be 255 or something lower. pctg is the percentage of elements in your row exceeding max brightness that causes you to delete the row. If even a single value over the max is too many, you can remove the sum statement entirely and just replace it with any(outOfBounds,2).
  2 件のコメント
vignesh
vignesh 2013 年 7 月 11 日
I want to throw out any row or column with saturated(255) pixel in it. this is exactly what i want.
Evan
Evan 2013 年 7 月 11 日
編集済み: Evan 2013 年 7 月 11 日
Okay, in that case, you can simplify the code down to:
outOfBounds = img == 255;
croppedImg = img;
croppedImg(any(outOfBounds,2),:) = [];
Does this code work for you?

サインインしてコメントする。

その他の回答 (1 件)

Dishant Arora
Dishant Arora 2013 年 7 月 11 日
I = imread('ImageName');
I = imcrop(I)
  2 件のコメント
vignesh
vignesh 2013 年 7 月 11 日
actually what i want to do is, if i have a row full of 255s(the white portion) i want want to delete the whole row otherwise retain it. same thing for column. so my cropped image will be more accurate. is that possible?
Dishant Arora
Dishant Arora 2013 年 7 月 11 日
J = (YourImage==255);
YourImage(:,all(J,1))='';
Yourimage(all(J,2),:)='';
Hope this is what you want.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by