Hello,
I have adjusted my code and am no longer getting any errors. When I am creating a histogram to find the center point of a white paper I am getting it a bit off-center.
Can anyone help?
Here is an image:
I would like the yellow dot in the center of the paper.
A tutorial I have been using: Finding Lane Curve | Self Driving Car with Lane Detection using Raspberry Pi p.4 - YouTube
My Code:
import cv2
import numpy as np
import Thresh
import ROI
def empty(x):
pass
def getHistogram(img,minPer=0.1,display=True):
histValues = np.sum(img,axis=0)
maxValue = np.max(histValues)
minValue = minPer*maxValue
indexArray = np.where(histValues>= minValue)
basePoint = int(np.average(indexArray))
w = img.shape[1]
h = img.shape[0]
print(type(w))
if display:
imgHist = np.zeros((img.shape[0],img.shape[1],3),np.uint8)
for x,intensity in enumerate(histValues):
temp=0
for i in intensity:
temp+= int(i)
cv2.line(imgHist,(int(x),int(w)),(int(x),int(h)-temp//255),(255,0,255),1)
cv2.circle(imgHist,(basePoint,int(h)),20,(0,255,255),cv2.FILLED)
return basePoint,imgHist
return basePoint
print(basePoint)
curveList = []
avgVal=10
def getCurve(frame):
# Step 1: Threhold Paper
frame,mask,result = Thresh.thresh(frame)
frameWPoints = frame.copy()
warpThresh = mask.copy()
warpFrame = frame.copy()
# Step 2: Define ROI
hT, wT, c = frame.shape
points = ROI.valTrackbars()
# Step 3: Warp Image
warpFrame = ROI.warpImg(warpFrame,points,wT,hT)
warpThresh = ROI.warpImg(warpThresh,points,wT,hT)
imgWarpPoints = ROI.drawPoints(frameWPoints,points)
# Step 4: Apply Histogram - Summation of Pixels
basePoint,imgHist = getHistogram(warpThresh)
# Step 5: Calculate Curve
# print(curveRaw)
# Step 6: Filter Curve
# Step 7: Display
#print(curve)
complete = frame
curve = 0
return int(curve),frame,mask,warpFrame,warpThresh,imgWarpPoints,imgHist,complete
if __name__ == "__main__":
cv2.namedWindow("HSV")
cv2.resizeWindow("HSV", 640, 240)
cv2.createTrackbar("HUE Min", "HSV", 0, 255, empty)
cv2.createTrackbar("HUE Max", "HSV", 179, 255, empty)
cv2.createTrackbar("SAT Min", "HSV", 0, 255, empty)
cv2.createTrackbar("SAT Max", "HSV", 255, 255, empty)
cv2.createTrackbar("VALUE Min", "HSV", 113, 255, empty)
cv2.createTrackbar("VALUE Max", "HSV", 255, 255, empty)
intialTrackBarVals = [50, 108, 0, 140]
ROI.initializeTrackbars(intialTrackBarVals)
vid = cv2.VideoCapture(0)
while(1):
_,frame = vid.read()
frame = cv2.resize(frame,(480,240))
curve,frame,mask,warp,warpThresh,roi,histogram,complete = getCurve(frame)
l1 =np.concatenate((frame,mask),axis=1)
l2 =np.concatenate((roi,warp,warpThresh),axis=1)
l3 =np.concatenate((histogram,complete),axis=1)
cv2.imshow("THRESH",l1)
cv2.imshow("TRANSFORM",l2)
cv2.imshow("RESULT",l3)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()
Thanks in advance,
Dev_101