Posts

Showing posts with the label Image Processing

Convert Np.array Of Type Float64 To Type Uint8 Scaling Values

Answer : A better way to normalize your image is to take each value and divide by the largest value experienced by the data type. This ensures that images that have a small dynamic range in your image remain small and they're not inadvertently normalized so that they become gray. For example, if your image had a dynamic range of [0-2] , the code right now would scale that to have intensities of [0, 128, 255] . You want these to remain small after converting to np.uint8 . Therefore, divide every value by the largest value possible by the image type , not the actual image itself. You would then scale this by 255 to produced the normalized result. Use numpy.iinfo and provide it the type ( dtype ) of the image and you will obtain a structure of information for that type. You would then access the max field from this structure to determine the maximum value. So with the above, do the following modifications to your code: import numpy as np import cv2 [...] info = np.iinfo(d...

Calculating Just A Specific Property In Regionprops Python

Answer : There seems to be a more direct way to do the same thing using regionprops with cache=False . I generated labels using skimage.segmentation.slic with n_segments=10000 . Then: rps = regionprops(labels, cache=False) [r.area for r in rps] My understanding of the regionprops documentation is that setting cache=False means that the attributes won't be calculated until they're called. According to %%time in Jupyter notebook, running the code above took 166ms with cache=False vs 247ms with cache=True , so it seems to work. I tried an equivalent of the other answer and found it much slower. %%time ard = np.empty(10000, dtype=int) for i in range(10000): ard[i] = size(np.where(labels==0)[1]) That took 34.3 seconds. Here's a full working example comparing the two methods using the skimage astronaut sample image and labels generated by slic segmentation: import numpy as np import skimage from skimage.segmentation import slic from skimage.data import a...

Alternative Segmentation Techniques Other Than Watershed For Soil Particles In Images

Image
Answer : You could try using Connected Components with Stats already implemented as cv2.connectedComponentsWithStats to perform component labeling. Using your binary image as input, here's the false-color image: The centroid of each object can be found in centroid parameter and other information such as area can be found in the status variable returned from cv2.connectedComponentsWithStats . Here's the image labeled with the area of each polygon. You could filter using a minimum threshold area to only keep larger polygons Code import cv2 import numpy as np # Load image, Gaussian blur, grayscale, Otsu's threshold image = cv2.imread('2.jpg') blur = cv2.GaussianBlur(image, (3,3), 0) gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # Perform connected component labeling n_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh, connectivity=4) # Create fal...