Giter Site home page Giter Site logo

computer-vision-stuff's Introduction

Let Xplore - Saturday Hacknight TinkerHub

Open CV

Cropping

img = img[10:300,250:300]

Drawing Lines

img = np.zeros((512,512,3),np.uint8)
cv.line(img,(0,0),(512,512),(0,255,255),5)
cv.line(img,(0,512),(512,0),(0,255,255),5)

Drawing Shapes

img = np.zeros((512,512,3),np.uint8)
cv.rectangle(img,(10,10),(250,350),(255,0,255),cv.FILLED)
cv.circle(img,(256,256),100,(255,255,0),cv.FILLED)
cv.putText(img,"OPEN CV",(100,100),cv.FONT_HERSHEY_COMPLEX,2,(0,100,0),2)

Warp Perspective

import cv2 as cv
import numpy as np

img = cv.imread('img/cards.jpg')

WIDTH,HEIGHT = 418,552
points1 = np.float32([[516,138],[934,295],[310,690],[690,856]])
points2 = np.float32([[0,0],[WIDTH,0],[0,HEIGHT],[WIDTH,HEIGHT]])

matrix = cv.getPerspectiveTransform(points1,points2)
imgWrap = cv.warpPerspective(img,matrix,(WIDTH,HEIGHT))

cv.imshow("Original Image",img)
cv.imshow("Wrap Image",imgWrap)

cv.waitKey(0)

image

Face Detection Snippets

import cv2 as cv

faceCascade = cv.CascadeClassifier(f'{cv.data.haarcascades}haarcascade_frontalface_default.xml')

#add your image path containing faces.
path = ''

img = cv.imread(path)

#converting the image to gray scale
imgGray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)

#extracting the images inside the input image using the trained model.
faces = faceCascade.detectMultiScale(imgGray,1.1,4)

#drawing an outing box over each shape.
for (x,y,w,h) in faces:
    cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

cv.imshow("Image",img)
cv.waitKey(0)

Shape Identifier Snippets

import cv2 as cv
import numpy as np


def getContours(img):
    global imgContour
    contours,hierarchy = cv.findContours(img,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)
    for cnt in contours:
        area = cv.contourArea(cnt)
        if area>500:
            cv.drawContours(imgContour,cnt,-1,(255,0,255),2)
            peri = cv.arcLength(cnt,True)
            # print(peri)
            approx = cv.approxPolyDP(cnt,0.02*peri,True)
            # print(len(approx))
            objCor = len(approx)
            x,y,w,h = cv.boundingRect(approx)

            if objCor ==3:
                ObjectType = "Tri"
            elif objCor == 4:
                aspectRatio = w/float(h)
                if aspectRatio>0.95 and aspectRatio <1.05:
                    ObjectType = "Square"
                else:
                    ObjectType = "Rectangle"
            elif objCor == 5 :
                ObjectType = "Pentagon"
            else:
                ObjectType ="Cicle"
            cv.rectangle(imgContour,(x,y),(x+w,y+h),(0,255,0))
            cv.putText(imgContour,ObjectType,(x+(w//2)-10,y+(h//2)-10),cv.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),1)


img = cv.imread('img/shapes.jpg')
img = cv.resize(img,(img.shape[1]-400,img.shape[0]-400))
imgContour = img.copy()

imgGray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
imgBlur = cv.GaussianBlur(imgGray,(11,11),1)
imgCanny = cv.Canny(imgBlur,50,50)

# cv.imshow("Original Image",img)
# cv.imshow("Gray Image",imgGray)
# cv.imshow("Blur Image",imgBlur)
# cv.imshow("Canny Image",imgCanny)


getContours(imgCanny)
cv.imshow("Contour Image",imgContour)


cv.waitKey(0)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.