hi dears I work on a project that finding and tracking altitude and azimuth sun angles with image processing. A camera take photo from shadows of a 10cm tail as my object in front of sun every 10 minutes. but when daytime is first morning or last evening receive a challenge and that is finding of tails shadow of the object on a paper. This line(shadow), when it is faint_colored or pale like this
it does not detect line. But when the line is rich-colored or strong my algorithm find the line(shadow). I need coordinates of top and bottom of line to calculate the length of line.
My codes are:
import time
import numpy as np
import cv2
import serial
from math import atan, sqrt, degrees
# Initialize the serial port
ser = serial.Serial('COM3', baudrate=9600, timeout=1)
def captureImage():
print('Capturing image')
videoCaptureObject = cv2.VideoCapture(1)
result = True
while(result):
ret, frame = videoCaptureObject.read()
cv2.imwrite("Newpicture.jpg", frame)
result = False
videoCaptureObject.release()
return frame
def processImage(im):
print('Processing image')
image = im
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Convert grayscale image to binary using Otsu thresholding
# Apply edge detection
edges = cv2.Canny(blurred, 50, 150)
# Find contours in the edge-detected image
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours:
# Get the largest contour (assuming it's the line)
c = max(contours, key=cv2.contourArea)
# Get the extreme points of the contour (line)
x1, y1 = c[c[:, :, 0].argmin()][0]
x2, y2 = c[c[:, :, 0].argmax()][0]
# Calculate the length of the line
length = sqrt((x2 - x1)**2 + (y2 - y1)**2)
I change the value of gaussianblur and edge values but do not solve my problem and do not detect lines. I use another model of camera with more quality but does not detect line. finally i find my problems in my codes.
You could try an adaptive threshold that handles variations in the lighting, along these lines:
import cv2 as cv
# Load image as greyscale
img = cv.imread('line.jpg', cv.IMREAD_GRAYSCALE)
# Threshold relative to brightness of local 49x49 area
th = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV,49,10)
# Save result
cv.imwrite('result.png', th)