現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How mat2gray work in matlab and How can I scale image using mat2gray as the format, 0 for max value and 255 for min value?
16 ビュー (過去 30 日間)
古いコメントを表示
first of all i want to know how mat2gray works ?. i mean back end work and formula etc. then i want scale the image using it so that it follows the format, 0 for max value and 255 for min value.
Thanks
回答 (2 件)
Guillaume
2017 年 7 月 8 日
You can look at the code of mat2gray and understand how it works by typing
edit mat2gray
at the command line. You'll find that it works exactly as explained in the documentation.
As for your other question, 0 for max value and 255 for min value, unfortunately for you, matlab follows the same mathematical rules as everybody else, where 0 < 255. You will have to write your own version of matlab if you want 255 < 0 (or a different definition of min and max). Perhaps if you explained your ultimate goal we may understand better why you ask such a question.
1 件のコメント
Image Analyst
2017 年 7 月 8 日
I'm not sure what " 0 for max value and 255 for min value" means, but you can invert your image like this:
invertedGrayImage = 255 - grayImage;
What used to be the max will now show up as 0 and imshow() will show it as dark/black.
Alternatively, you could use a colormap, which doesn't change the image or create a new one, it changes the way it appears upon display
imshow(grayImage); % Scale min to max
colormap(gca, flipud(gray(256)));
mat2gray scales the min to 0 and the max to 255, no matter where they started. To create a new array where the min starts at 0 and the max gets mapped to 255, do this:
uint8Image = uint8(255 * mat2gray(grayImage));
23 件のコメント
Walter Roberson
2017 年 7 月 9 日
I prefer
uint8Image = im2uint8(1 - mat2gray(grayImage));
When you use uint8(255*X) then you only get 1/2 bucket for the value 0 and 1/2 bucket for the value 1 because of rounding.
sufian ahmed
2017 年 7 月 9 日
編集済み: sufian ahmed
2017 年 7 月 9 日
@image analyst, sir i want to scale image using mat2gray.by deafault it considers 0 for min value of image and 1 for max value of image.but i just do it in inverse order. for example: it scales as: maximage value=0 and minimage value=255. how can i ?.. what modification need in mat2gary. if i want to inverse it. Thanks
Image Analyst
2017 年 7 月 9 日
Pass 255-grayImage into mat2gray instead of grayImage to invert the brightness.
Walter can you give some code showing where there is a difference in the output images, because I can't think of a situation off the top of my head?
% Create a uint8 image where every gray level occurs 10 times.
grayImage = uint8(repmat([0:255], [10, 1]));
uint8Image1 = uint8(255 * mat2gray(255 - grayImage));
uint8Image2 = im2uint8(1 - mat2gray(grayImage));
theyreEqual12 = isequal(uint8Image1, uint8Image2)
% Create a double image where every gray level occurs 10 times.
grayImage = repmat([0.25: 0.5 : 9.75], [10, 1]);
uint8Image3 = uint8(255 * mat2gray(255 - grayImage));
uint8Image4 = im2uint8(1 - mat2gray(grayImage));
theyreEqual34 = isequal(uint8Image3, uint8Image4)
histObj3 = histogram(uint8Image3, 0:255);
counts3 = histObj3.Values
histObj4 = histogram(uint8Image4, 0:255);
counts4 = histObj4.Values
For the above code, both your code and my code give the same number of output pixels at 0 and 255.
sufian ahmed
2017 年 7 月 9 日
@image analyst. this is not RGB image and it is a depth image in uint16 format. In this case, how inverted this image ? I think 255-gray ,in this case not work ? m i right ?
Walter Roberson
2017 年 7 月 9 日
Hmmm... I know I've seen a difference between im2uint8(x) vs uint8(255*x) in the past; I cannot seem to replicate it at the moment.
Walter Roberson
2017 年 7 月 9 日
sufian ahmed, my suggestion of
uint8Image = im2uint8(1 - mat2gray(grayImage));
should work for your grayImage being uint16
Image Analyst
2017 年 7 月 9 日
Yes that shoudl work. A handy function to know if you want to know the max possible value for the data type of your image is intmax, so
maxPossibleGrayLevel = intmax(class(yourImage));
It will return 255 or 65535 depending on if your image is uint8 or uint16, respectively.
sufian ahmed
2017 年 7 月 10 日
編集済み: sufian ahmed
2017 年 7 月 10 日
@imageanlyst then it will be final code : uint8Image1 = uint8(255 * mat2gray(maxPossibleGrayLevel - grayImage));
for mapping uint16 depth image into 0-255 using inverse order ?
sufian ahmed
2017 年 7 月 10 日
編集済み: sufian ahmed
2017 年 7 月 10 日
@ walter , i dont undersatnd how u r code works. please explain ? is it work for me if i want to map, 0 for max value nad 255 for min value for uint16 depth image ? for knowledge i want, how mat2gray scalize the value, which formula it uses ?
Walter Roberson
2017 年 7 月 10 日
編集済み: Walter Roberson
2017 年 7 月 10 日
mat2gray = @(A) double((A-min(A(:)))./double(max(A(:))-min(A(:)))
If you really want to do everything in one step then you could do
uint8(255*double((max(A(:))-A-min(A(:)))./double(max(A(:))-min(A(:))))
Guillaume
2017 年 7 月 10 日
how mat2gray scalize the value
As already stated in my answer, you can look at the code of mat2gray to understand exactly how it works. It works exactly as explained in its documentation, so it's not clear what you don't understand.
sufian ahmed
2017 年 7 月 10 日
@walter sir, thanks... i understand ... now tell me one thing... my max depth image value is 8000 and this image is uint16 that means 0 to 65530 scale range. now if i map this depth image value 0 to 255 considering 0 for maxVal and 255 for minval. how can i modify it ?
Walter Roberson
2017 年 7 月 10 日
In the situation you describe, the code I posted would map 8000 to 0, and would map 0 to 255, and would map everything else linearly between those. In other words, the code already examines the data that is present in the image and maps the largest data value in the image to 0 and maps the smallest data value (probably 0) to 255.
If you want 65535 (largest possible uint16 value rather than largest value in your image) mapped to 0, and 0 mapped to 255, then you could use
uint8Image = uint8(255 * mat2gray(grayImage,[0 65535]));
Raka Mukherjee
2020 年 1 月 27 日
I have an image of 7731x7561 uint16 dimension. The highest value is 7699 and the lowest value is 0. How can I map the value between 0-255 for processing the image further? I tried with the im2uint8 function but that does not give unique values as only 4 to 5 values cover up the entire range due to loss of information. Is there a way to get 255 unique values where 0 shall be the lowest value and 7699 shall be the highest ,i.e. 255? I want to play with the colour of the images, as I have 3 bands of R, G and B of the same dimension.
Walter Roberson
2020 年 1 月 27 日
mat2gray() to scale it to the range 0 to 1, then im2uint8() to scale that to 0 to 255.
Is there a way to get 255 unique values
You told us that there are only 4 to 5 unique values in the input. How should the 4 to 5 unique values be split into 255 unique values?
Image Analyst
2020 年 1 月 27 日
You can do
uint8Image = uint8(255 * mat2gray(yourImage));
but why? What's wrong with leaving it as uint16, and leaving it in the original range with original values? But like Walter said, if you have only 4 or 5 values before scaling, you'll still have 4 or 5 values after scaling, not 255.
Raka Mukherjee
2020 年 1 月 28 日
Thank you so much for your answers. No I said about the 4 to 5 values after perfoming the im2uint8 function.
Walter Roberson
2020 年 1 月 28 日
I thought that I had suggested rgb2ind on the original followed by ind2rgb with a different colormap.
Your values peak at just below 8192 which is 2^13. If you have a bunch of different values that are within 32 of each other and so would become the same when the image is scaled to 256 by division by 32, then it is not obvious how you would represent them as different 8 bit values -- unless, that is, you are willing to let go of the correct relative scaling of values?
Raka Mukherjee
2020 年 1 月 28 日
Sorry I did'nt get you. On what basis can we say that a bunch of values are within 32 of each other?Why should we divide it with 32?
Guillaume
2020 年 1 月 28 日
@Raka, Can you start your own question? It would be a much better place to help you than the comments of an answer to a completely unrelated question.
Image Analyst
2020 年 1 月 28 日
Yes, I agree with Guillaume. And attach your image file and answer my question above about why you can't just work with the original values.
Raka Mukherjee
2020 年 1 月 29 日
Yes sure. I am starting my own question and attaching the images as well. I am quite new to MATLAB and more new to work with the colour component of images so I am not quite sure whether we can work with a 16 bit data to find out particular colour (say,gray) from images as it is easier to work with 8 bit data. The images are Landsat Red, Green and Blue bands which have a depth of unsigned 16 bit.
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)