OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How does OpenCV detect line? [closed]

  • Thread starter Thread starter isa nemati
  • Start date Start date
I

isa nemati

Guest
I have a problem finding the line on a paper. This line, when it is faint_colored or pale like this

enter image description here

it does not detect line. But when the line is rich-colored or strong my algorithm find the line. I need the x, y of the top and bottom of the line to extract data from image.

My codes are:

Code:
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 answer and do not detect lines. I use another model of camera that also does not detect. line.
<p>I have a problem finding the line on a paper. This line, when it is faint_colored or pale like this</p>
<p><a href="https://i.sstatic.net/Yj4EttNx.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Yj4EttNx.jpg" alt="enter image description here" /></a></p>
<p>it does not detect line. But when the line is rich-colored or strong my algorithm find the line. I need the x, y of the top and bottom of the line to extract data from image.</p>
<p>My codes are:</p>
<pre><code>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)
</code></pre>
<p>I change the value of gaussianblur and edge values but do not answer and do not detect lines. I use another model of camera that also does not detect. line.</p>
 

Latest posts

Z
Replies
0
Views
1
zuegelwagen
Z
Top