Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used:

import cv2
import numpy as np
img = cv2.imread('1.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 110, 255, cv2.THRESH_BINARY)
cv2.imwrite('2.jpg', threshold)
img = cv2.imread('2.jpg')
threshold = (255 - threshold)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,200,minLineLength,maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('3.jpg', img)
cv2.imshow('hough',img)
cv2.waitKey(0)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used:

import cv2
import numpy as np
img = cv2.imread('1.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 110, 255, cv2.THRESH_BINARY)
cv2.imwrite('2.jpg', threshold)
img = cv2.imread('2.jpg')
threshold = (255 - threshold)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,200,minLineLength,maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('3.jpg', img)
cv2.imshow('hough',img)
cv2.waitKey(0)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used:

import cv2
import numpy as np
img = cv2.imread('1.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 110, 255, cv2.THRESH_BINARY)
cv2.imwrite('2.jpg', threshold)
img = cv2.imread('2.jpg')
threshold = (255 - threshold)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,200,minLineLength,maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('3.jpg', img)
cv2.imshow('hough',img)
cv2.waitKey(0)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used:

import cv2
import numpy as np
img = cv2.imread('1.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 110, 255, cv2.THRESH_BINARY)
cv2.imwrite('2.jpg', threshold)
img = cv2.imread('2.jpg')
threshold = (255 - threshold)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,200,minLineLength,maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('3.jpg', img)
cv2.imshow('hough',img)
cv2.waitKey(0)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used:used and this tutorial and this function:

import cv2
import numpy as np
from skimage.morphology import skeletonize

def find_skeleton3(img):
    skeleton = np.zeros(img.shape,np.uint8)
    eroded = np.zeros(img.shape,np.uint8)
    temp = np.zeros(img.shape,np.uint8)

    retval,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))

    iters = 0
    while(True):
        cv2.erode(thresh, kernel, eroded)
        cv2.dilate(eroded, kernel, temp)
        cv2.subtract(thresh, temp, temp)
        cv2.bitwise_or(skeleton, temp, skeleton)
        thresh, eroded = eroded, thresh # Swap instead of copy

        iters += 1
        if cv2.countNonZero(thresh) == 0:
            return (skeleton,iters)

img = cv2.imread('1.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 110, 255, cv2.THRESH_BINARY)
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('2.jpg', threshold)
img = cv2.imread('2.jpg')
 threshold = (255 - threshold)
cv2.bitwise_not(threshold)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,200,minLineLength,maxLineGap)
cv2.HoughLinesP(threshold,1,np.pi/180,255,minLineLength,maxLineGap)
img = np.ones((3000,4000,3), np.uint8)
img[img==1]=255
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.line(img,(x1,y1),(x2,y2),(0,0,0),8)
cv2.imwrite('3.jpg', img)
cv2.imshow('hough',img)
cv2.waitKey(0)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
esqueleto, iters = find_skeleton3(img)
cv2.imwrite('4.jpg', esqueleto)

The result is:

ResultResult

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used and this tutorial and this function:

import cv2
import numpy as np
from skimage.morphology import skeletonize

def find_skeleton3(img):
    skeleton = np.zeros(img.shape,np.uint8)
    eroded = np.zeros(img.shape,np.uint8)
    temp = np.zeros(img.shape,np.uint8)

    retval,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))

    iters = 0
    while(True):
        cv2.erode(thresh, kernel, eroded)
        cv2.dilate(eroded, kernel, temp)
        cv2.subtract(thresh, temp, temp)
        cv2.bitwise_or(skeleton, temp, skeleton)
        thresh, eroded = eroded, thresh # Swap instead of copy

        iters += 1
        if cv2.countNonZero(thresh) == 0:
            return (skeleton,iters)

img = cv2.imread('1.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 110, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('2.jpg', threshold)

threshold = cv2.bitwise_not(threshold)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,255,minLineLength,maxLineGap)
img = np.ones((3000,4000,3), np.uint8)
img[img==1]=255
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,0,0),8)
cv2.imwrite('3.jpg', img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
esqueleto, iters = find_skeleton3(img)
cv2.imwrite('4.jpg', esqueleto)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used and this tutorial and this function:

import cv2
import numpy as np
from skimage.morphology import skeletonize

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

def find_skeleton3(img):
    # https://stackoverflow.com/a/42846932/7690982
    skeleton = np.zeros(img.shape,np.uint8)
    eroded = np.zeros(img.shape,np.uint8)
    temp = np.zeros(img.shape,np.uint8)

    retval,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))

    iters = 0
    while(True):
        cv2.erode(thresh, kernel, eroded)
        cv2.dilate(eroded, kernel, temp)
        cv2.subtract(thresh, temp, temp)
        cv2.bitwise_or(skeleton, temp, skeleton)
        thresh, eroded = eroded, thresh # Swap instead of copy

        iters += 1
        if cv2.countNonZero(thresh) == 0:
            return (skeleton,iters)

img = cv2.imread('1.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imread('C:\\Users\\Desktop\\teste\\1.jpg')
#HSV
#https://stackoverflow.com/a/47483966/7690982
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (36, 0, 0), (70, 255,255))
## slice no verde
imask = mask>0
verde = np.zeros_like(img, np.uint8)
verde[imask] = img[imask]
cv2.imwrite('C:\\Users\\Desktop\\teste\\2.jpg', verde)

#Threshold
(canal_h, canal_s, canal_v) = cv2.split(verde)
retval, threshold = cv2.threshold(grayscaled, 110, cv2.threshold(canal_v, 130, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('2.jpg', threshold)

threshold = cv2.bitwise_not(threshold)
cv2.imwrite('C:\\Users\\Desktop\\teste\\3.jpg', canal_v)

#Hough
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,255,minLineLength,maxLineGap)
img = np.ones((3000,4000,3), np.uint8)
img[img==1]=255
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,0,0),8)
cv2.imwrite('3.jpg', cv2.line(img,(x1,y1),(x2,y2),(0,0,0),15)
cv2.imwrite('C:\\Users\\Desktop\\teste\\4.jpg', img)

Skeletonize

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) esqueleto, iters = find_skeleton3(img) cv2.imwrite('4.jpg', esqueleto) esqueleto = cv2.dilate(esqueleto,kernel,iterations = 4) cv2.imwrite('C:\Users\Desktop\teste\5.jpg', esqueleto)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used and this tutorial and this function:

import cv2
import numpy as np

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

def find_skeleton3(img):
    # https://stackoverflow.com/a/42846932/7690982
    skeleton = np.zeros(img.shape,np.uint8)
    eroded = np.zeros(img.shape,np.uint8)
    temp = np.zeros(img.shape,np.uint8)

    retval,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))

    iters = 0
    while(True):
        cv2.erode(thresh, kernel, eroded)
        cv2.dilate(eroded, kernel, temp)
        cv2.subtract(thresh, temp, temp)
        cv2.bitwise_or(skeleton, temp, skeleton)
        thresh, eroded = eroded, thresh # Swap instead of copy

        iters += 1
        if cv2.countNonZero(thresh) == 0:
            return (skeleton,iters)

img = cv2.imread('C:\\Users\\Desktop\\teste\\1.jpg')
#HSV
#https://stackoverflow.com/a/47483966/7690982
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (36, 0, 0), (70, 255,255))
## slice no verde
imask = mask>0
verde = np.zeros_like(img, np.uint8)
verde[imask] = img[imask]
cv2.imwrite('C:\\Users\\Desktop\\teste\\2.jpg', verde)

#Threshold
(canal_h, canal_s, canal_v) = cv2.split(verde)
retval, threshold = cv2.threshold(canal_v, 130, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('C:\\Users\\Desktop\\teste\\3.jpg', canal_v)

#Hough
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,255,minLineLength,maxLineGap)
img = np.ones((3000,4000,3), np.uint8)
img[img==1]=255
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,0,0),15)
cv2.imwrite('C:\\Users\\Desktop\\teste\\4.jpg', img)

Skeletonize

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) esqueleto, iters = find_skeleton3(img) esqueleto = cv2.dilate(esqueleto,kernel,iterations = 4) cv2.imwrite('C:\Users\Desktop\teste\5.jpg', esqueleto)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used and this tutorial and this function:

import cv2
import numpy as np

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

def find_skeleton3(img):
    # https://stackoverflow.com/a/42846932/7690982
    skeleton = np.zeros(img.shape,np.uint8)
    eroded = np.zeros(img.shape,np.uint8)
    temp = np.zeros(img.shape,np.uint8)

    retval,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))

    iters = 0
    while(True):
        cv2.erode(thresh, kernel, eroded)
        cv2.dilate(eroded, kernel, temp)
        cv2.subtract(thresh, temp, temp)
        cv2.bitwise_or(skeleton, temp, skeleton)
        thresh, eroded = eroded, thresh # Swap instead of copy

        iters += 1
        if cv2.countNonZero(thresh) == 0:
            return (skeleton,iters)

img = cv2.imread('C:\\Users\\Desktop\\teste\\1.jpg')
#HSV
#https://stackoverflow.com/a/47483966/7690982
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (36, 0, 0), (70, 255,255))
## slice no verde
imask = mask>0
verde = np.zeros_like(img, np.uint8)
verde[imask] = img[imask]
cv2.imwrite('C:\\Users\\Desktop\\teste\\2.jpg', verde)

#Threshold
(canal_h, canal_s, canal_v) = cv2.split(verde)
retval, threshold = cv2.threshold(canal_v, 130, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('C:\\Users\\Desktop\\teste\\3.jpg', canal_v)

#Hough
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,255,minLineLength,maxLineGap)
img = np.ones((3000,4000,3), np.uint8)
img[img==1]=255
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,0,0),15)
cv2.imwrite('C:\\Users\\Desktop\\teste\\4.jpg', img)

Skeletonize

#Skeletonize img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) esqueleto, iters = find_skeleton3(img) esqueleto = cv2.dilate(esqueleto,kernel,iterations = 4) cv2.imwrite('C:\Users\Desktop\teste\5.jpg', esqueleto)

cv2.imwrite('C:\\Users\\Desktop\\teste\\5.jpg', esqueleto)

The result is:

Result

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?

Draw lines on a field

With this image:

colored field

A threshold was applied:

threshold

And the expected result would be:

Expected Result

With the following code being used and this tutorial and this function:

import cv2
import numpy as np

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

def find_skeleton3(img):
    # https://stackoverflow.com/a/42846932/7690982
    skeleton = np.zeros(img.shape,np.uint8)
    eroded = np.zeros(img.shape,np.uint8)
    temp = np.zeros(img.shape,np.uint8)

    retval,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))

    iters = 0
    while(True):
        cv2.erode(thresh, kernel, eroded)
        cv2.dilate(eroded, kernel, temp)
        cv2.subtract(thresh, temp, temp)
        cv2.bitwise_or(skeleton, temp, skeleton)
        thresh, eroded = eroded, thresh # Swap instead of copy

        iters += 1
        if cv2.countNonZero(thresh) == 0:
            return (skeleton,iters)

img = cv2.imread('C:\\Users\\Desktop\\teste\\1.jpg')
#HSV
#https://stackoverflow.com/a/47483966/7690982
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (36, 0, 0), (70, 255,255))
## slice no verde
imask = mask>0
verde = np.zeros_like(img, np.uint8)
verde[imask] = img[imask]
cv2.imwrite('C:\\Users\\Desktop\\teste\\2.jpg', verde)

#Threshold
(canal_h, canal_s, canal_v) = cv2.split(verde)
retval, threshold = cv2.threshold(canal_v, 130, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('C:\\Users\\Desktop\\teste\\3.jpg', canal_v)

#Hough
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(threshold,1,np.pi/180,255,minLineLength,maxLineGap)
img = np.ones((3000,4000,3), np.uint8)
img[img==1]=255
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,0,0),15)
cv2.imwrite('C:\\Users\\Desktop\\teste\\4.jpg', img)

#Skeletonize
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
esqueleto, iters = find_skeleton3(img)
esqueleto =  cv2.dilate(esqueleto,kernel,iterations = 4)
cv2.imwrite('C:\\Users\\Desktop\\teste\\5.jpg', esqueleto)

The result is:

ResultResult

What would be the best approach to solve this?

Multiple Ransac? Hough Transform? Maybe reduce the size of the blobs?