이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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?
조회 수: 14 (최근 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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)