MATLAB image algebra operation exception handling

					——前言——

There are two types of abnormal operations: calculation result overflow and calculation result type invalid.
① Calculation result overflow:
Many images, eg: grayscale image, indexed color image, binary image or limited-bit true color image, the pixels are all limited in range. When we perform addition, subtraction, multiplication, and division operations on multiple images, the calculation result is likely to exceed the finite value range. For example, when two 256-color grayscale images are subtracted, the result may be negative; when the addition is performed, the pixels exceed 255. These are calculation results overflow.
②The calculation result type is invalid:
there are many types of image data. When we use uint8, uint16, such as uint8, uint16, when the pixel is required to be an integer, the result may be a fraction. This is because the image algebraic operation function often treats the data as a double type when performing operations.

The correction of abnormal calculation results follows two principles:
Results that exceed the valid range of integer types are directly truncated to the end of the limited range.
[Using uint8 as an example of the condition]
eg: The theoretical result is -12, and the revised result is 0; the theoretical result is 687, and the revised result is 255.
②Round the score calculation.

Now use an example to demonstrate the calculation result of a new image obtained by adding and subtracting a picture.
Example: Use matrix addition to increase or decrease a certain color component in the image.
clear all;

a=imread(‘C:\图片\qiqi.png’);
s=size(a);
b=double(a);
c(:,:,1)=b(:,:,1)+b(:,:,2);
c(:,:,2)=b(:,:,2);
c(:,:,3)=b(:,:,3)-b(:,:,2);
for i=1:s(1)
for j=1:s(2)
for k=1:s(3)
if c(i,j,k)<0
c(i,j,k)=0;
end
if c(i,j,k)>255
c(i,j,k)=255;
end
end
end
end

c=uint8( c );
subplot(121);imshow(a);
subplot(122);imshow( c );

Result: Insert picture description here
tip: what does matlab A=(:,1) and T =(:,:,1) mean?
A(x,y) represents the element at the position of the xth row and yth column of the two-dimensional matrix, and x is: means all rows. Therefore, A(:,1) means all the elements in the first column of A, which is a column vector.
T =(:,:,1): matlab T=(:,:,1) represents all the data in the first and second dimensions, the first in the third dimension.

Guess you like

Origin blog.csdn.net/weixin_44842715/article/details/109407686