import cv2 import numpy as np import matplotlib.pyplot as plt # Function to load and preprocess the image def preprocess_image(image_path): # Read the image in grayscale img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # Apply Gaussian Blur to reduce noise img_blurred = cv2.GaussianBlur(img, (5, 5), 0) # Apply adaptive thresholding to detect bands _, thresh_img = cv2.threshold(img_blurred, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) return thresh_img # Function to detect bands in the gel def detect_bands(thresh_img): # Find contours in the thresholded image (these correspond to the bands) contours, _ = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Filter small contours that may be noise bands = [cnt for cnt in contours if cv2.contourArea(cnt) > 100] return bands # Function to plot the bands def plot_bands(image_path, bands): # Load the original image for visualization img = cv2.imread(image_path) # Draw the detected bands on the image img_bands = img.copy() for band in bands: cv2.drawContours(img_bands, [band], -1, (0, 255, 0), 2) # Convert the image from BGR to RGB for matplotlib img_bands_rgb = cv2.cvtColor(img_bands, cv2.COLOR_BGR2RGB) # Display the image with detected bands plt.imshow(img_bands_rgb) plt.axis('off') plt.show() # Function to measure the intensity of bands def measure_band_intensity(image_path, bands): img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) intensities = [] for band in bands: # Create a mask for each band mask = np.zeros_like(img) cv2.drawContours(mask, [band], -1, 255, thickness=cv2.FILLED) # Calculate the average intensity inside the band mask band_intensity = cv2.mean(img, mask)[0] intensities.append(band_intensity) return intensities # Main program if __name__ == "__main__": # Path to the gel electrophoresis image image_path = 'gel_image.jpg' # Change this to your image file path # Preprocess the image thresh_img = preprocess_image(image_path) # Detect bands in the image bands = detect_bands(thresh_img) # Plot the bands on the original image plot_bands(image_path, bands) # Measure the intensity of each detected band intensities = measure_band_intensity(image_path, bands) # Display the intensity values for each band for i, intensity in enumerate(intensities): print(f"Band {i+1} intensity: {intensity}")
devendra bairwa
devendra.snc11011 at gmail.com
Tue Mar 18 16:41:25 UTC 2025
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Function to load and preprocess the image
def preprocess_image(image_path):
# Read the image in grayscale
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Apply Gaussian Blur to reduce noise
img_blurred = cv2.GaussianBlur(img, (5, 5), 0)
# Apply adaptive thresholding to detect bands
_, thresh_img = cv2.threshold(img_blurred, 0, 255,
cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
return thresh_img
# Function to detect bands in the gel
def detect_bands(thresh_img):
# Find contours in the thresholded image (these correspond to
the bands)
contours, _ = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# Filter small contours that may be noise
bands = [cnt for cnt in contours if cv2.contourArea(cnt) >
100]
return bands
# Function to plot the bands
def plot_bands(image_path, bands):
# Load the original image for visualization
img = cv2.imread(image_path)
# Draw the detected bands on the image
img_bands = img.copy()
for band in bands:
cv2.drawContours(img_bands, [band], -1, (0, 255, 0), 2)
# Convert the image from BGR to RGB for matplotlib
img_bands_rgb = cv2.cvtColor(img_bands, cv2.COLOR_BGR2RGB)
# Display the image with detected bands
plt.imshow(img_bands_rgb)
plt.axis('off')
plt.show()
# Function to measure the intensity of bands
def measure_band_intensity(image_path, bands):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
intensities = []
for band in bands:
# Create a mask for each band
mask = np.zeros_like(img)
cv2.drawContours(mask, [band], -1, 255,
thickness=cv2.FILLED)
# Calculate the average intensity inside the band mask
band_intensity = cv2.mean(img, mask)[0]
intensities.append(band_intensity)
return intensities
# Main program
if __name__ == "__main__":
# Path to the gel electrophoresis image
image_path = 'gel_image.jpg' # Change this to your image
file path
# Preprocess the image
thresh_img = preprocess_image(image_path)
# Detect bands in the image
bands = detect_bands(thresh_img)
# Plot the bands on the original image
plot_bands(image_path, bands)
# Measure the intensity of each detected band
intensities = measure_band_intensity(image_path, bands)
# Display the intensity values for each band
for i, intensity in enumerate(intensities):
print(f"Band {i+1} intensity: {intensity}")
More information about the Digitalmars-d
mailing list