简体   繁体   中英

Matlab Code only Calculating number of pixels and not their sum, if pixel value falls above given Threshold value

K = imread('test.jpg');                               // read image 
countif=0;countelse=0;                                // declare variables
addif=0;addelse=0;
counttotal=0; temp=0;        
[M N] = size(K);                                      // calculate the size of the image
for x=1:M                                             // X-Axis
    for y=1:N                                         // Y-Axis

                temp=K(x,y);                          // Pixel Value at location(x,y)

                if (temp<50)                          // If value of pixel is below threshold value 50 
                    addif =  addif + temp;            // Add to get sum of all these pixels
                    countif = countif + 1;            // Counter to get total number of such pixels 


                else                                  // If pixel value is above threshold limit i.e 50
                 countelse = countelse + 1;           // Add to get sum of all these pixels
                 addelse=   addelse + temp;           // Counter to get total number of such pixels 
                end

        counttotal=counttotal+1;                     // Total rotation counter
    end
end

Here in this code, given above, counters named 'countif' and 'countelse' are working properly but values in variables 'addif' and 'addelse' are not as it should be.

You can use logical indexing for this

tresholdMask = (K < 50); %array of the same size as k with 1s where K<50 and 0s elsewhere
numOfPixels = sum(thresholdMask(:)); %number of 1s in the mask
sumOfPixels = sum(K(:).*thresholdMask(:)); %sum of all the pixels where the mask is 1

Solution:-

Replace this: K = imread('test.jpg'); with this: K = double(imread('test.jpg')) ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍

Or if you want to keep the unit8 class of K and temp , instead of above fix, do the following: ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍Replace this addif = addif + temp; with this: addif = addif + double(temp); ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍ ‍ ‍ ‍‍‍‍‍‍ ‍ ‍‍‍‍‍‍and this addelse= addelse + temp; with this: addelse= addelse + double(temp);

Explanation:-

Class of temp is uint8 , because class of K is uint8 therefore classes of your resulting addif and addelse also become unit8 which cannot store value more that 255 . By converting K into double , all your next variables using it, become double as I did in first proposed solution. Or if you want to keep the classes of K and temp same, you can use the second proposed solution.

You can check the class of variables by using whos in command window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM