Showing posts with label NumPy. Show all posts
Showing posts with label NumPy. Show all posts

Friday, July 4, 2025

How do you extract glucose data from a glucose CGM report using Python?

 In my previous post, I described how to find the graph of glucose vs. time using a blue mask (the curve was in blue). This post is about graphing Glucose data vs. time using the curve from the previous post.

The main result of the previous post resulted in developing a mask’ for the curve as shown here.

This Python script (GlucoseCalibrated.py) that follows is designed to extract numerical glucose data from a graph image by analyzing the blue glucose trace, calibrating it to real-world glucose values, and then saving the results. The important determinations that are to be made are, 1) Correctly rendering the Y-axis of the displayed curve and establishing the Glucose data in mg/dL correctly to the numerical data from the calculation. In associating the correct Glucose values, the measurements on the curve from the report were made using GIMP. The X-axis association with time has to be done as well.

import cv2
import numpy as np
import matplotlib.pyplot as plt
import csv
import os

# --- Configuration ---

IMAGE_PATH = "LibreViewOneDayGraph.jpg"

LOWER_BLUE = np.array([100, 50, 50])
UPPER_BLUE = np.array([140, 255, 255])

# --- CRITICAL CALIBRATION PARAMETERS ---
GLUCOSE_VALUE_1 = 0
PIXEL_Y_1 = 170

GLUCOSE_VALUE_2 = 150
PIXEL_Y_2 = 107
# ----------------------------------------

print(f"Attempting to load image from: {IMAGE_PATH}. "
f"Current working directory: {os.getcwd()}")

# Load image
img = cv2.imread(IMAGE_PATH)

# Check if the image was loaded successfully
if img is None:
print(f"Error: Could not load image from {IMAGE_PATH}. "
"Please check the path and name.")
else:
print(f"Image '{IMAGE_PATH}' loaded successfully. "
f"Dimensions: {img.shape}")
img_height, img_width, _ = img.shape
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Create mask for the blue color
mask = cv2.inRange(img_hsv, LOWER_BLUE, UPPER_BLUE)
print(f"Mask created. Number of blue pixels detected: "
f"{np.count_nonzero(mask)}")
if np.count_nonzero(mask) == 0:
print("Warning: No blue pixels found in the image with the "
"current HSV range. Check image color or HSV values.")

# Create a copy of the image to draw on (for visualization)
img_with_circles = img.copy()
circle_radius = 2

# List to store x, y coordinates and glucose values for CSV
glucose_trace_data = []
glucose_trace_data.append(
['x_pixel', 'y_pixel_raw', 'glucose_value_mg_dL'])

# --- Calculate the linear transformation parameters (m and b) ---
m_glucose_scale = (GLUCOSE_VALUE_2 - GLUCOSE_VALUE_1) / \
(PIXEL_Y_2 - PIXEL_Y_1)
b_glucose_offset = GLUCOSE_VALUE_1 - \
(m_glucose_scale * PIXEL_Y_1)

print(f"\n--- Calibration Results ---")
print(f"Using calibration points: ({PIXEL_Y_1}px -> "
f"{GLUCOSE_VALUE_1}mg/dL) and ({PIXEL_Y_2}px -> "
f"{GLUCOSE_VALUE_2}mg/dL)")
print(f"Calculated Glucose Scale (m): {m_glucose_scale:.4f}")
print(f"Calculated Glucose Offset (b): {b_glucose_offset:.4f}")
print(f"---------------------------\n")

# Iterate through each x-position to find the glucose trace
print("Starting trace detection loop...")
for x_pos in range(img_width):
column = mask[:, x_pos]
y_hits = np.where(column > 0)[0]

if len(y_hits) > 0:
y_pixel_raw = int(np.median(y_hits))

actual_glucose_value = \
(m_glucose_scale * y_pixel_raw) + b_glucose_offset
actual_glucose_value_rounded = round(actual_glucose_value, 2)

cv2.circle(img_with_circles, (x_pos, y_pixel_raw),
circle_radius, (0, 0, 255), -1)

glucose_trace_data.append(
[x_pos, y_pixel_raw, actual_glucose_value_rounded])
print(f"Trace detection loop finished. Data points collected: "
f"{len(glucose_trace_data) - 1}")

# --- Display the visualized image ---
plt.figure(figsize=(12, 7))
plt.imshow(cv2.cvtColor(img_with_circles, cv2.COLOR_BGR2RGB))
plt.title("Detected Glucose Trace on Original Image "
"(with Calibration Notes)")
plt.axis("off")
plt.show()

# Save the image with circles
output_image_filename = "glucose_trace_detected.jpg"
cv2.imwrite(output_image_filename, img_with_circles)
print(f"Visualized trace image saved to {output_image_filename}")

# --- Save data to CSV file ---
csv_filename = "glucose_trace_data_calibrated_values.csv"
try:
with open(csv_filename, 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerows(glucose_trace_data)
print(f"Glucose trace data saved to {csv_filename}")
except IOError:
print(f"Error: Could not write to {csv_filename}. "
"Please check file permissions or if the file is open.")

print("\nScript finished execution.")

Here's a step-by-step summary of its functionality:

  1. Import Libraries: It imports cv2 (OpenCV) for image processing, numpy for numerical operations, matplotlib.pyplot for displaying images, csv for writing data to CSV files, and os for path-related diagnostics.
  2. Configuration:
    • IMAGE_PATH: Specifies the path to your input glucose graph image (e.g., "LibreViewOneDayGraph.jpg"). This is the image the script will analyze.
    • LOWER_BLUE, UPPER_BLUE: These define a range in the HSV color space (Hue, Saturation, Value) that precisely represents the "blue" color of your glucose trace. Any pixels falling within this range are considered part of the trace.
  3. Critical Calibration Parameters:
    • GLUCOSE_VALUE_1, PIXEL_Y_1: These are your first calibration point. GLUCOSE_VALUE_1 is a known glucose level (e.g., 0 mg/dL), and PIXEL_Y_1 is the exact Y-pixel coordinate you manually measured from the image corresponding to that glucose level.
    • GLUCOSE_VALUE_2, PIXEL_Y_2: These are your second calibration point, representing another known glucose level (e.g., 150 mg/dL) and its corresponding Y-pixel coordinate.
    • Purpose: These two points are crucial for establishing a linear relationship between pixel Y-coordinates and actual glucose values.
  4. Image Loading and Initial Checks:
    • The script attempts to load the image specified by IMAGE_PATH.
    • It checks if the image loaded successfully. If not, it prints an error and stops.
    • It converts the image from BGR (Blue, Green, Red - OpenCV's default) to HSV (Hue, Saturation, Value) color space, as HSV is generally better for color-based segmentation.
  5. Color Masking:
    • cv2.inRange(img_hsv, LOWER_BLUE, UPPER_BLUE) creates a binary mask. This mask is a black-and-white image where white pixels represent areas of the original image that fall within the defined blue color range (i.e., your glucose trace).
    • It prints the number of detected blue pixels as a diagnostic.
  6. Calibration Calculation:
    • It calculates the m (slope) and b (y-intercept) values for a linear equation: Glucose_Value = m * raw_y_pixel + b.
    • m_glucose_scale: Represents how many mg/dL each pixel unit corresponds to. It's calculated using the two calibration points. It's typically negative because a higher pixel Y-value (further down the image) means a lower glucose value on the graph.
    • b_glucose_offset: The offset, calculated using one of the calibration points and the derived slope.
    • These values form the core of converting pixel data to meaningful glucose readings.
  7. Glucose Trace Detection Loop:
    • The script iterates through every single vertical column (x_pos) of the image, from left to right.
    • For each column, it looks at the mask to find all y_hits (white pixels, indicating blue color from the trace).
    • np.median(y_hits): If blue pixels are found in a column, it takes the median Y-coordinate of those pixels. Using the median helps to make the detection robust against noise or slight variations in line thickness.
    • Calibration Application: The y_pixel_raw (median Y-coordinate) is then fed into the linear equation (m * y_pixel_raw + b) to get the actual_glucose_value_rounded.
    • Visualization: A red circle is drawn on a copy of the original image (img_with_circles) at the (x_pos, y_pixel_raw) to visually confirm where the trace was detected.
    • Data Storage: The x_pos, y_pixel_raw, and actual_glucose_value_rounded are stored in the glucose_trace_data list.
  8. Output and Saving:
    • Display Image: The img_with_circles (original graph with red dots on the detected trace) is displayed using matplotlib.
    • Save Image: This visualized image is also saved as glucose_trace_detected.jpg.
    • Save CSV: The glucose_trace_data (containing all the detected x, y, and calibrated glucose values) is saved to a CSV file named glucose_trace_data_calibrated_values.csv.

In essence, this script automates the process of "reading" your glucose graph by color detection and translating the visual curve into a precise set of numerical data points.

Here is the result of running this code:

Attempting to load image from: LibreViewOneDayGraph.jpg. Current working directory: C:\Users\hoden\PycharmProjects\GraphtoData

Image 'LibreViewOneDayGraph.jpg' loaded successfully. Dimensions: (244, 1140, 3)

Mask created. Number of blue pixels detected: 5325


--- Calibration Results ---

Using calibration points: (170px -> 0mg/dL) and (107px -> 150mg/dL)

Calculated Glucose Scale (m): -2.3810

Calculated Glucose Offset (b): 404.7619

---------------------------

Starting trace detection loop...

Trace detection loop finished. Data points collected: 1091

Visualized trace image saved to glucose_trace_detected.jpg

Glucose trace data saved to glucose_trace_data_calibrated_values.csv

Script finished execution.

Process finished with exit code 0

The CSV file is written starting at the beginning of the curve to the end and at each point a red dot is placed on the original to unambiguosly verify that the whole curve is covered. 


Thursday, February 27, 2025

Can you create animation with Python? Part 1

It is possible to create simple animation with Python easily. This post shows how easy to create simple animation such as a GIF.

As the first step we create a wheel with a single spoke. We will then extend it to create a wheel with 5 spokes. 

The code for creating a circle with one spoke is as shown:

--------------------------------

from PIL import Image, ImageDraw

import numpy as np


def create_one_spoke_image(filename="one_spoke.png"):

    img_size = 500

    center = (img_size // 2, img_size // 2)

    radius = img_size // 4


    img = Image.new("RGB", (img_size, img_size), "white")

    draw = ImageDraw.Draw(img)


    # Draw the circle

    draw.ellipse((center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius), fill="black")


    # Draw ONE spoke (at 0 degrees for simplicity)

    x1 = center[0]

    y1 = center[1]

    x2 = center[0] + radius  # Spoke extends to the right

    y2 = center[1]

    draw.line((x1, y1, x2, y2), fill="red", width=3)


    img.save(filename)

    print(f"Saved: {filename}")

    img.show()


if __name__ == "__main__":

    create_one_spoke_image()

------------------------------------------------------

Here is the complete explanation of the code:


from PIL import Image, ImageDraw

import numpy as np

from PIL import Image, ImageDraw:

This line imports the Image and ImageDraw classes from the Python Imaging Library (PIL), also known as Pillow.

Image is used to create and manipulate image objects.

ImageDraw is used to draw shapes and lines on those image objects.

import numpy as np:

While numpy is imported, in this specific code provided, it is not actually utilized. If future modifications were to be made to the code, and array manipulation was needed, numpy would then be used.

2. Creating a Blank Image:

img_size = 500

img = Image.new("RGB", (img_size, img_size), "white")

img_size = 500:

This sets the size of the image to 500 pixels by 500 pixels.

img = Image.new("RGB", (img_size, img_size), "white"):

This creates a new image object.

"RGB" specifies that the image will be in RGB color mode (red, green, blue).

(img_size, img_size) sets the dimensions of the image.

"white" sets the initial background color of the image to white.

3. Creating a Draw Object:

draw = ImageDraw.Draw(img)

draw = ImageDraw.Draw(img):

This creates a Draw object associated with the img image.

The Draw object provides methods for drawing shapes, lines, and text onto the image.

4. Drawing a Circle:

center = (img_size // 2, img_size // 2)

radius = img_size // 4

draw.ellipse((center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius), fill="black")

center = (img_size // 2, img_size // 2):

This calculates the center coordinates of the image.

radius = img_size // 4:

This calculates the radius of the circle, which is one-quarter of the image size.

draw.ellipse(...):

This draws an ellipse (which will be a circle in this case, due to the equal width and height).

The coordinates (center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius) define the bounding box of the ellipse.

fill="black" sets the fill color of the circle to black.

5. Drawing a Spoke:

x1 = center[0]

y1 = center[1]

x2 = center[0] + radius  # Spoke extends to the right

y2 = center[1]

draw.line((x1, y1, x2, y2), fill="red", width=3)

x1 = center[0], y1 = center[1]:

These set the starting coordinates of the line (the spoke) to the center of the image.

x2 = center[0] + radius, y2 = center[1]:

These set the ending coordinates of the line. The spoke extends horizontally to the right, to the edge of the circle.

draw.line(...):

This draws a line between the starting and ending coordinates.

fill="red" sets the color of the line to red.

width=3 sets the thickness of the line to 3 pixels.

6. Saving the Image:

img.save(filename)

print(f"Saved: {filename}")

img.show()

img.save(filename):

This saves the created image to a file with the specified filename.

print(f"Saved: {filename}"):

This prints a confirmation message to the console.

img.show(): This line displays the image on a related printer as shown.

--------------------------------------------------------

When you run this code you will see the following:

C:\Users\hoden\AppData\Local\Programs\Python\Python312\python.exe C:\Users\hoden\PycharmProjects\exploreImage\Images\CircleOneSpoke.py 
Saved: one_spoke.png
Process finished with exit code 0

The following image is displayed:








Thursday, December 26, 2024

Ever Explored an Image? Dive In Now!


If you have not, it is time you do it. Python lends itself to be the way to unveil the magic behind images. While python is the main tool you need libraries to help along.

There is a famous adage, if you want to control something you need to measure it first. In order to change or use image in a yet to unknown ways, you need to unravel an image. Image processing is all about how in myriad ways you can use images for aesthetic, scientific and medical fields. Python with a plethora of image and other supportive libraries such as PIL, OpenCV, NumPy, etc. is great for this purpose. First of all we need to understand Pixel, it is like understanding a point in geometry, the fundamental building block.

What is a pixel?

The unveiling of an image starts with the building block of images, the pixels - geometrical structures with very minute dimensions that have some color in them.

Pixels are actually square. Each pixel is a tiny square of color that, when combined with other pixels, forms the images you see on screens. This grid of square pixels is what allows digital displays to render images with precision and clarity. The concept of pixels being square is fundamental to how digital images are created and displayed. In a sense, pixels are abstract that depend on the device on which we see the image. The physical size of a pixel is device dependent. The physical size of a pixel has to be derived from a knowledge of the resolving power of the device, the dots per inch seen on the device. While in Micro LEDs they are about 50 microns they can be fraction of a millimeter in large LEDs.  

Images are a grid of pixels, each with a specific color. When viewed together, these pixels form the images we see.

What is NumPy and what is its role in image processing?

In the process of understanding images, NumPy plays a crucial role. This library helps to separate an image into three distinct parts: height, width, and color. Furthermore, it isolates the colors red, green, and blue, placing them into their respective channels. By dismantling the image with NumPy, we can access the five important components of an image. These components can be manipulated using various image libraries in Python.

Also, while NumPy can disassemble an image, it can also reconstruct an image and it is extremely versatile. NumPy depends for its speed on the core C programming constructs but implemented for a high level language like Python.

Finding the height and width of an image using NumPy in Python:

This program takes in an image and find its shape with three arguments, height, width and number of channels. You can also see the result of running this code in PyCharm.

This image Kiss.jpg has height and width of 7401 and 7376 with three colors.

Finding the height, width and colors:

The following code unravels the image (the python program is using PyCharm interface):



1. When this code is run the image dimensions (height and width) are returned.

2. It separates the 3 color channels into its constituents.

3. It gives three images and saves them to a files in the same project

In arriving at these results it has,

1. Reduced the size of image by resizing so that final image is not distorted, visible and without any display related artifacts. (When this code was run without resizing, it returned 3 very large images all in grey).

2. After resizing, I have extracted the three channel information. Remember, it is zero-based.

3. Each channel (red, green, and blue) is populated by values from the image in a two-step process. In the first step, an array with the same dimensions as the image is created, but all elements are initialized to zero for the chosen channel. In the second step, this array is filled with the values of the pertinent color from the image.

4. It creates separate images of the same image in separate colors, one each for red, green and blue and shows them.

5. It also writes to file, the images for each of these channels.

As you can see, we have the height, width and three separate images of the image looking at ony red, green and blue values in its pixels.






Here is the code, should you try to run for your image:

========================================

import cv2
import numpy as np

# Load the image
image = cv2.imread(r'C:\Users\hoden\PycharmProjects\exploreImage\Images\TheKiss.jpg')
# Get the dimensions of the image
height, width, channels = image.shape

# Print the dimensions
print(f'Width: {width} pixels, Height: {height} pixels')

# resizing the image to display smaller sizes on my display device
resized_image = cv2.resize(image, (200, 200))

# Extract individual color channels
blue_channel = resized_image[:, :, 0]
green_channel = resized_image[:, :, 1]
red_channel = resized_image[:, :, 2]

# Create separate images for each channel
blue_image = np.zeros_like(resized_image)
blue_image[:, :, 0] = blue_channel

green_image = np.zeros_like(resized_image)
green_image[:, :, 1] = green_channel # Assign green channel to green image

red_image = np.zeros_like(resized_image)
red_image[:, :, 2] = red_channel

# Display the extracted channels as images
cv2.imshow('Blue Channel', blue_image)
cv2.imshow('Green Channel', green_image)
cv2.imshow('Red Channel', red_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Save the extracted channels as images (optional)
cv2.imwrite('blue_channel.jpg', blue_image)
cv2.imwrite('green_channel.jpg', green_image)
cv2.imwrite('red_channel.jpg', red_image)



 

 





Sunday, December 22, 2024

You have seen a GIF, have you created one?

  You have seen a GIF image and enjoyed it. But have you created one.? There are software programs to create GIFs and perhaps many online sites help creating GIFs.  Here is one for example, https://www.canva.com/create/gif-maker/. Animation akin to GIFS can also be created using javascript although there may not be resulting image with a .gif extension.

GIFs are like very small duration video clips but they are really a composite of many small images flipped rapidly, in-situ to create the illusion of motion. They are made to impress you by their motion.

Why GIFS?

Imagine bringing life to a still photo of a sunset by animating the clouds. An image may be worth a thousand words, but a GIF is worth a thousand images. It adds dynamic movement to static images, unleashing your creativity. If you're active on social media, GIFs are an invaluable tool for self-expression, offering endless possibilities. They bring humor, emotion, and laughter to any page, transforming a collection of static images into a vibrant and engaging experience. With GIFs, you can elevate your storytelling to new heights!"

Here is a simple GIF image copied from a web site  (display gif on web browser - Search):


Ins and outs of using Python to create GIFs:

GIF images have the .gif extension which stands for Graphics Interchange Format. 

In this post, I show you how to create from scratch a GIF using Python. Python uses a image library such as PIL to get the images into the program such as PyCharm. Once they are in the program, you need to use another library to do the flipping action. This library is called imageio, image input/output program. For GIFs this program takes in the images (a number of them) and then it customizes the duration each image is shown; whether the gif repeats itself and how many times it does, etc. Note: if you are new to PyCharm, read up my other Python/Image related posts in this blog, http://hodentekhelp.blogpost.com.

The program presented here also goes through the indexing of the images (as there are many images) which necessitate calling yet another useful library called NumPy. The imageio program cannot accept a image list, but a Numpy list and hence the use of the NumPy library.

In order to create a gif file in a python program, you need the following:

1. A set of images. 

2. Importing libraries PIL, ImageIO and Numpy.

Here is are some five image files I am using in the program:






These are not the best, but enough for the purpose.

Here is the python program that creates a GIF image taking in the images shown here:

----------------------------

BirdsGIF.py

-----------------

# Import libraries used to create a GIF image

import imageio

import numpy as np

from PIL import Image

from PIL.ImageFile import ImageFile

# Load images

image1: ImageFile = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_3\Bird1.jpg')

image2 = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_3\Bird2.jpg')

image3 = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_3\Bird3.jpg')

image4 = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_3\Bird4.jpg')

image5 = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_3\Bird5.jpg')

# Resize images (optional)

image1 = image1.resize((200, 200))

image2 = image2.resize((200, 200))

image3 = image3.resize((200,200))

image4 = image4.resize((200,200))

image5 = image5.resize((200,200))

# Create list of images

images = [image1, image2, image3, image4, image5]

# Convert PIL image  list to NumPy arrays

images = [np.array(img) for img in images]

# Create the GIF using the image list

imageio.mimsave('my_animation.gif', images, duration=2000.0, loop=5)

----------------------

The above program creates a image file with .gif extension and images show for 2000 milliseconds and the GIF image runs five times in succession. If you have time elapsed stills of any event you can easily turn it into an amazingly dynamic image.

That is all there is to creating a GIF. If your images are made with care and are of good quality you can indeed create a good GIF.

The GIf file created, my_animation.gif can be seen in Photo app on your windows computer device, however you can just drag and drop the image on a browser should also work.


Mahalo for staying with me. 

p.s: For these holidays, you can create your greeting card with a GIF. Try it!






Sunday, December 15, 2024

Have you considered image augmentation in image data generation - Part 1?

 Python has emerged as a programming language of choice when it comes to generating images through image augmentation techniques. Image augmentation techniques can provide a rich diversity of the training dataset and turbocharge the image recognition models. 

This post explores one of the fundamental image data augmentation techniques, random cropping. A future post discusses the random clipping.

Cropping and flipping are common image manipulation techniques that don't require explicit redefinition. They are often implemented using built-in functions or libraries like OpenCV and PIL.

Details of random cropping:

Here is the python code to randomly crop an image.

=========

def random_crop(img, crop_size):


    h, w = img.shape[:2]

    x = np.random.randint(0, w - crop_size[1])

    y = np.random.randint(0, h - crop_size[0])

    crop_img = img[y:y+crop_size[0], x:x+crop_size[1]]

    return crop_img

============================

The random cropping shown above does not directly use the OpenCV, it uses the functionality of NumPy arrays. 

Note:  Defining h, w =img.shape[:2] correctly picks the images height and width appropriate for NumPy array. The common channels of NumPy are [height, width, and  color-channel] in that order.

Also note:

NumPy arrays use zero-based indexing. This means the first element in an array has an index of 0, the second has an index of 1, and so on.

In the random_crop function:

x = np.random.randint(0, w - crop_size[1])

This line generates a random starting x-coordinate for the crop. w is the width of the image.

crop_size[1] is the width of the cropped region.

By subtracting crop_size[1] from w, we ensure that the starting x-coordinate plus the crop width doesn't exceed the image's width.

y = np.random.randint(0, h - crop_size[0])

This line generates a random starting y-coordinate for the crop.

h is the height of the image. crop_size[0] is the height of the cropped region.

Similarly, this ensures the starting y-coordinate plus the crop height doesn't exceed the image's height.

Random cropping is the same as cropping with randomness introduced to the randomness in choosing the cropping coordinates.

The following code is just regular cropping of an image:

====================

from PIL import Image

def crop_image(image_path, left, top, right, bottom):
"""
Crops an image using PIL.

Args:
image_path: Path to the image file.
left: X-coordinate of the top-left corner of the cropping region.
top: Y-coordinate of the top-left corner of the cropping region.
right: X-coordinate of the bottom-right corner of the cropping region.
bottom: Y-coordinate of the bottom-right corner of the cropping region.

Returns:
The cropped image as a PIL Image object.
"""
try:
img = Image.open(image_path)
cropped_img = img.crop((left, top, right, bottom))
return cropped_img
except Exception as e:
print(f"Error cropping image: {e}")
return None


# Example usage:
image_path = r"C:\Users\hoden\PycharmProjects\exploreImage\Images\TheKiss.jpg"
left = 100
top = 50
right = 300
bottom = 200

cropped_image = crop_image(image_path, left, top, right, bottom)

if cropped_image:
cropped_image.show()
cropped_image.save("cropped_image.jpg")

Now, Let us consider random cropping:

TBC--> TO BE CONTIUED

Friday, February 16, 2024

What is Scikit-image?

 This latest Blogpost is by our valued guest author, Sriraksha V. Raghavan.

Image analysis, commonly known as computer vision, has made significant advances with the help of AI. However, there are still many challenges to overcome. The use of deep learning and neural networks in the analysis of images by AI is helping to narrow this gap. Currently, AI models are improving in areas such as object detection, image recognition, and generating images from text descriptions.

There are several coding tools and libraries available for these tasks, including OpenCV, TensorFlow, PyTorch, Keras, Caffe, MATLAB, CUDA, Microsoft’s Cognitive Toolkit (CNTK), Scikit-image, Dlib, Mahotas, and others. The choice of tool depends on your specific requirements, such as the complexity of the task and the hardware available.

Meet our guest author Sriraksha, an electrical engineer specializing in image sensors and machine vision. In her free time, she enjoys reading about human visual systems and continental philosophy, and dabbles in writing. You can reach her at Sriraksha.v.raghavan@gmail.com.



One of the popular Python libraries for image processing is scikit-image, an open-source collection of algorithms hosted on GitHub(1). Scikit-image has an advantage over other libraries because of the speed of its algorithms, which is essential for working with large amounts of data.

To get started with scikit-image, some understanding of the NumPy library would be beneficial but not mandatory. The easiest way to install scikit-image is via pip in your command line window(2).

Scikit-image stores images as NumPy ndarrays(n-dimensional), which are arrays of numbers with rows, columns, and dimensions. The dimensions represent the color planes of the image, such as red, green, blue, infrared, x-ray, etc. Scikit-image has various functions and classes for image processing, ranging from simple tasks like changing the color scale to complex ones like image segmentation, feature detection, and image restoration.

The best way to learn scikit-image is to download its pre-existing dataset using the function call ski.data.download_all() in your Python environment. This will give you some image sets that you can manipulate using different scikit-image functions and classes. You can also follow the tutorials available on the scikit-image website(3), which use the same datasets to demonstrate how scikit-image works.

Additionally, some useful forums to discuss scikit-image applications are their Zulip page(4), a community forum where people post queries and share ideas, and their GitHub page(1), where you can access their API and codebase.

1: scikit-image/scikit-image: Image processing in Python (https://en.wikipedia.org/wiki/Scikit-)
2: Installation — scikit-image (https://scikit-image.org/docs/stable/user_guide/install.html)
3: Tutorials — scikit-image (https://scikit-image.org/docs/stable/user_guide/tutorials.html)
4: scikit-image - Zulip Chat Archive (https://zulip.com/help/view-images-and-videos)

If you are new to,
 'pip [https://hodentekhelp.blogspot.com/2018/09/how-do-you-upgrade-pip.html]' 
or 
'Numpy [https://hodentekhelp.blogspot.com/2020/06/what-is-numpy.html]' 
you can find them in this blog in addition to other python items.


Tuesday, June 23, 2020

What is NumPy?

NumPy is a basic N-dimensional array package

If you have installed Python you can run  'pip' to install Numpy. Search for 'Pip' to find the correct location off 'pip'.

------------------------------------------------------------------------------------------------------------------
If Python is installed (I have Python 3.8) then run this from CMD with elevated permissions.

C:\Users\Owner\AppData\Local\Programs\Python\Python38\Lib\site-packages>dir
 Volume in drive C has no label.
 Volume Serial Number is 9CA9-492A


-------------------------------------------------------------------------------------------------------------
For installing NumPy, run the following command.

C:\Users\Owner\AppData\Local\Programs\Python\Python38\Lib\site-packages>pip install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/72/dd/fcb5046365a1c3edd8e6d5824e58a1065682b90d475dceac0d55f68764c3/numpy-1.19.0-cp37-cp37m-win_amd64.whl (13.0MB)
     |████████████████████████████████| 13.0MB 6.8MB/s
Installing collected packages: numpy
  WARNING: The script f2py.exe is installed in 'C:\Users\Owner\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.19.0
WARNING: You are using pip version 19.3.1; however, version 20.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.