0

I'm trying to create a feature matching script using NoIR camera. What I'm doing is take a picture using and compare it with the real time video feed. Everything works fine but the the live video frame looks a little bit zoomed in. I googled but couldn't find a solution or reason for this problem.

See the pic attached: enter image description here

Image capturing code:

import picamera
from subprocess import call
from time import gmtime, strftime, sleep
# Our filename
fileName = strftime("images/image-%d-%m-%y_%H:%M:%S.png", gmtime())

# Take a picture using our camera
with picamera.PiCamera() as camera:
    camera.resolution = (480, 320)
    camera.framerate = 32
    camera.start_preview()
    camera.capture(fileName)
    camera.stop_preview()
    print("We have taken a picture.")

Feature matching code:

import numpy as np
import cv2
from matplotlib import pyplot as plt
import sched
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import datetime
from os import listdir
from os.path import isfile, join
starttime=time.time()
camera = PiCamera()
camera.resolution = (480, 320)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(480, 320))

# allow the camera to warmup
time.sleep(0.3)

# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

    QueryImgBGR = frame.array
    img1 = cv2.cvtColor(QueryImgBGR,cv2.COLOR_BGR2GRAY)
    orb = cv2.ORB_create()
    kpx = orb.detect(img1,None)
    kp1, des1 = orb.compute(img1, kpx)

    mypath='images'
    onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
    images = np.empty(len(onlyfiles), dtype=object)
    program_starts = datetime.datetime.now()
    for n in range(0, len(onlyfiles)):
        print(onlyfiles[n] )

        images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
        images[n] = cv2.cvtColor(images[n] , cv2.COLOR_BGR2GRAY)
        img2 = images[n]

        kpy = orb.detect(img2,None)
        kp2, des2 = orb.compute(img2, kpy)

        # create BFMatcher object
        bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

        # Match descriptors.
        matches = bf.match(des1,des2)
        goodMatch = sorted(matches, key = lambda x:x.distance)
        img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:200], None, flags=2)
        MIN_MATCH_COUNT = 150 #SM 130 seems a good limit
        if(len(goodMatch)<=MIN_MATCH_COUNT):
            ##SM print("No Match")
            print(len(goodMatch), MIN_MATCH_COUNT)
            print("  ")
        else:
            print("Match Found-------------------")

        cv2.imshow("Frame", img3)
        #cv2.imshow("x", img1)
        key = cv2.waitKey(1) & 0xFF

        # clear the stream in preparation for the next frame
        rawCapture.truncate(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.