def main(argv): default_file = 'images/board.JPEG' filename = argv[0] if len(argv) > 0 else default_file # Loads an image src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE) # Check if image is loaded fine if src is None: print('Error opening image!') print('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n') return -1 dst = cv.Canny(src, 50, 200, None, 3) # Copy edges to the images that will display the results in BGR cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR) cdstP = np.copy(cdst) lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0) if lines is not None: for i in range(0, len(lines)): rho = lines[i][0][0] theta = lines[i][0][1] a = math.cos(theta) b = math.sin(theta) x0 = a * rho y0 = b * rho pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a))) cv.line(cdst, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA) linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10) if linesP is not None: for i in range(0, len(linesP)): l = linesP[i][0] cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 3, cv.LINE_AA) y = int(round(1536 / 3)) x = int(round(2048 / 3)) src = cv.resize(cdst, (x, y)) # Resize image cdst = cv.resize(cdst, (x, y)) # Resize image cdstP = cv.resize(cdstP, (x, y)) # Resize image cv.imshow("Source", src) cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst) cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP) cv.imwrite("./houghLines.png", cdstP) cv.waitKey() return 0
def detect_face(path, layout): image = cv2.imread(path) height, width = image.shape[:2] resized = cv2.resize(image, (width / 5, height / 5)) faces = faceCascade.detectMultiScale(resized, scaleFactor=1.2, minNeighbors=5, minSize=(5, 5), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) (x, y, w, h) = faces[0] rect = faces[0] draw_rectangle(resized, rect) draw_text(resized, "Face", rect[0], rect[1] - 5) cv2.imwrite("/sdcard/face.jpg", resized) layout.views.preview.src = "file:///sdcard/face.jpg" return True
def track_x(x): cv.line(img, (x, 0), (x, h), RED, d) cv.line(img, (0, y), (w, y), RED, d) cv.imshow('window,img) def track_y(y): cv.line(img, (x, 0), (x, h), RED, d) cv.line(img, (0, y), (w, y), RED, d) cv.imshow('window,img) file = 'messi.jpg' # img = cv.imread(file, cv.IMREAD_GRAYSCALE) img0 = cv.imread(file, cv.IMREAD_COLOR) cv.imshow('window,img) w, h = 800, 600 x, y = 100, 100 d = 1 cv.createTrackbar('x', 'window, x, w, track_x) cv.createTrackbar('y', 'window, y, h, track_y) cv.line(img, (x, 0), (x, h), RED, d) cv.line(img, (0, y), (w, y), RED, d) cv.imshow('window,img) k = cv.waitKey(0)
import cv img = cv.imread("/Users/miura/img/blobs.tif") import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np #img=mpimg.imread('/Users/miura/img/blobs.tif') # numpy array to CV matrix #cvmat = cv.fromarray(img) #cvmat to numpyarray is a = np.asarray(img) #cv.DestroyAllWindows() imgplot = plt.imshow(a)
import cv as cv import numpy as np def normalize(img): minimum = np.amin(img) return (img - minimum) * (255 / np.amax(img)) out_path = "result/4.1.2/" baboon = cv.imread('image/Baboon.bmp', cv.IMREAD_GRAYSCALE) f16 = cv.imread('image/F16.bmp', 0) lena = cv.imread('image/Lena.bmp', 0) cv.imwrite(out_path + 'baboon_gray.jpg', baboon) cv.imwrite(out_path + 'f16_gray.jpg', f16) cv.imwrite(out_path + 'lena_gray.jpg', lena) # compute DFT of images baboon_f = np.abs(np.fft.fft2(baboon)) f16_f = np.abs(np.fft.fft2(f16)) lena_f = np.abs(np.fft.fft2(lena)) # shift baboon_f_shift = np.fft.fftshift(baboon_f) f16_f_shift = np.fft.fftshift(f16_f) lena_f_shift = np.fft.fftshift(lena_f) # log on no shifted image baboon_f_log = np.log(baboon_f + 1) f16_f_log = np.log(f16_f + 1)
if __name__ == '__main__': args, conf=pre() #load the image # grab the image paths and randomly shuffle them image_paths = sorted(list(paths.list_images(args['path']))) #begin to process for i in image_paths: # i is the path of single image iamge=cv.imread(i) name=os.path.split(i)[1]#split path to be name[1] and direction[0] image_located=locate_mechanism(image,name) #save the results path_out=args["output_path"]+name cv.imwrite(path_out,image_located)
if(cl < minC): minC = cl if(cl > maxC): maxC = cl b_box = [[minL, minC], [minL, maxC], [maxL, minC], [maxL, maxC]] return b_box '''TESTS''' """Exo 1 """ print('Exercice 1, La moyenne est : {moy}'.format(moy = average_above_zero([1,2,3,4,5]))) """Exo 1.1 """ print('Exercice 1.1, La valeur maximale est : {max}'.format(max = max_value_of_table([1,2,3,4,5]))) """Exo 1.2 """ max = max_of_table([1,2,3,4,5]) print('Exercice 1.2, L\'index de la valeur max est : {max_index} et la valeur max est : {max_value}'.format(max_index = max[1], max_value = max[0])) """Exo 2 """ print('Exercice 2, Le tableau inversé est : {table}'.format(table = reverse_table([1,2,3,4,5]))) """Exo 3 """ img=cv2.imread("../s3/trounoir.jfif",0) roi = roi_bbox(img) print('Exercice 3, la bounding box est de coordonnées : lmax = {lmax}, lmin = {lmin}, cmax = {cmax}, cmin = {cmin}'.format(lmax = roi[3][0], lmin = roi[0][0], cmax = roi[3][1], cmin = roi[0][1]))
# -*- coding: utf-8 -*- """ Created on Thu Jul 25 08:49:37 2019 @author: novanto """ import os import cv import cv2 PATH = "C:/Users/novanto/Downloads/AI AU Materials/FINAL PROJECT 4" data_path = PATH + "/test_images" data_dir_list = os.dirlist(data_path) for i in data_dir_list: img1 = cv.imread(data_path + "/" + i) img2 = cv2.resize(img1, (224, 224), interpolation=cv2.INTER.CUBIC) cv2.imwrite(data_path + "/" + i, img2) print(data_path + "/" + i)
import androidhelper import cv import socket droid = androidhelper.Android() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('enter your own same as in server', 5005)) while True: flag = s.recv(4096) if flag == 'ok': droid.cameraCapturePicture('/sdcard/caps/cap.jpg') img = cv.imread('/sdcard/caps/cap.jpg') img = cv.resize(img, (200, 200)) img_str = cv.imencode('.jpg', img)[1].tostring() print len(img_str) s.send(img_str) flag2 = s.recv(4096) s.close()
import cv as cv2 import numpy as np img = cv2.imread('opencv_logo.png', 0) img = cv2.medianBlur(img, 5) cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0) circles = np.uint16(np.around(circles)) for i in circles[0, :]: # draw the outer circle cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2) # draw the center of the circle cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3) cv2.imshow('detected circles', cimg) cv2.waitKey(0) cv2.destroyAllWindows()
print('active: ', active) return droid, uuid path = '/storage/7E9B-5A00/Picture/' #serial通信オープン droid, uuid = serialStart() cnt = 0 while True: # 写真を取る droid.cameraCapturePicture(path + str(cnt) + 'image.png') # openCVで開く img = cv.imread(path + str(cnt) + 'image.png') mask = red_detect(img) # 赤色検出 cv.imwrite(path + str(cnt) + 'Redmask.png', mask) # 保存 # 面積を求める pix_area, per = calc_area(mask) # 重心を求める x, y = calc_center(mask) print("G({},{})".format(x, y)) # 差分を取る if cnt >= 1: # 1つ前の画像の計算 img0 = cv.imread(path + str(cnt - 1) + 'image.png') mask0 = red_detect(img0) _, per0 = calc_area(mask0)
import cv import pytesseract # Mention the installed location of Tesseract-OCR in your system pytesseract.pytesseract.tesseract_cmd = 'System_path_to_tesseract.exe' # Read image from which text needs to be extracted img = cv.imread("sample.jpg") # Preprocessing the image starts # Convert the image to gray scale gray = cv.cvtColor(img, cv2.COLOR_BGR2GRAY) # Performing OTSU threshold ret, thresh1 = cv.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) # Specify structure shape and kernel size. # Kernel size increases or decreases the area # of the rectangle to be detected. # A smaller value like (10, 10) will detect # each word instead of a sentence. rect_kernel = cv.getStructuringElement(cv2.MORPH_RECT, (18, 18)) # Appplying dilation on the threshold image dilation = cv.dilate(thresh1, rect_kernel, iterations=1) # Finding contours contours, hierarchy = cv.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
import cv, cv2 img = cv.imread("cones_plus_shadowed_barrel.jpg", cv2.CV_LOAD_IMAGE_COLOR) cv.namedWindow('Normal Image') cv.imshow('Normal Image', img) hsv_img = img cv.CvtColor(img,hsv_img,cv.CV_RGB2HSV) cv.namedWindow('HSV Image') cv.imshow('HSV Image', hsv_img) cv.waitKey(0) cv.destroyAllWindows()
def cameraCapture(self, filename): '''Androidで写真を撮って保存する''' droid.cameraCapturePicture(path + str(filename) + '.png') img = cv.imread(path + str(filename) + '.png') return img
import numpy as np import cv # Create a black image #img = np.zeros((512,512,3), np.uint8) img = cv.imread('great.jpg', 1) # Draw a diagonal blue line with thickness of 5 px img = cv.line(img,(0,0),(511,511),(255,0,0),5) cv.imwrite('blessed.jpg', img)
import cv as cv2 import numpy as np img = cv2.imread('opencv_logo.png',0) img = cv2.medianBlur(img,5) cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20, param1=50,param2=30,minRadius=0,maxRadius=0) circles = np.uint16(np.around(circles)) for i in circles[0,:]: # draw the outer circle cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) # draw the center of the circle cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) cv2.imshow('detected circles',cimg) cv2.waitKey(0) cv2.destroyAllWindows()
import cv import numpy as np from matplotlib import pyplot as plt img=cv.imread("Desktop/ayushi.jpg") mask=np.zeroes(img.shape[:],np.uint8) bgdModel=np.zeroes((1,65
# Resize the image for faster processing img = Image.open(cam_cap) reduced_percent = 0.1 out = img.resize([int(reduced_percent * s) for s in img.size]) imagePath = "/sdcard/image.jpg" out.save(imagePath) sleep(3) # Set the haarcascade file path cascPath = "/sdcard/haarcascade_frontalface_default.xml" # Create the haar cascade faceCascade = cv2.CascadeClassifier(cascPath) # Read the image image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Detect faces in the image faces = faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) print("Found {0} faces!".format(len(faces))) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) #save the image
import cv #to use relative path, note that the working directory is the main qpython folder #to use absolute path, just copy and paste the path from your file manager srcPath = 'great.jpg' desPath = 'call.jpg' img = cv.imread(srcPath, 0) cv.imwrite(desPath, img)
import numpy as np import cv # Load an color image in grayscale img = cv.imread('pooja-2.jpg', 0)
import cv from androidhelper import Android droid = Android() path = "/sdcard/qpython/photos/test.jpg" droid.cameraCapturePicture(path) #droid.cameraInteractiveCapturePicture(path) img = cv.imread(path) cv.imwrite('/sdcard/qpython/photos/cvImage.jpg', img)
img_filtering = img_fft * cv.resize(np.abs(this_filter), (new_height, new_width)) # shift img_filtering = np.fft.ifftshift(img_filtering) # IDFT and real img_ifft = np.real(np.fft.ifft2(img_filtering)) # resize image final_img = np.zeros((height, width)) for i in range(height): for j in range(width): final_img[i][j] = img_ifft[i][j] return final_img out_path = "result/4.1.1/" barbara_gray = cv.imread('image/Barbara.bmp', cv.IMREAD_GRAYSCALE) cv.imwrite(out_path + 'barbara_gray.jpg', barbara_gray) # a filter a = np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) a = a / 16 # separate a a_separate_c = np.array([1, 2, 1]) a_separate_c = a_separate_c / 4 a_separate_r = np.array([[1, 2, 1]]) a_separate_r = a_separate_r / 4 # b filter b = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])