Home > OS >  How make eye and nose bigger or smaller in opencv and python
How make eye and nose bigger or smaller in opencv and python

Time:11-20

I used the following code to select nose in OpenCV and Python i searched a lot of to find a way to change the size of nose and save as a other image but i didn't find anything is there anybody to help me to do this.

import cv2
import numpy as np
import dlib
img = cv2.imread('1.jpg')
img = cv2.resize(img,(0,0),None,0.5,0.5)
imgOriginal = img.copy()

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

def createBox(img,points,scale=5):
  bbox = cv2.boundingRect(points)
  x,y,w,h = bbox
  imgCrop = img[y:y h,x:x w]
  imgCrop = cv2.resize(imgCrop,(0,0),None,scale,scale)
  return imgCrop


imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = detector(imgGray)
for face in faces:
  x1,y1 = face.left(),face.top()
  x2,y2 = face.right(),face.bottom()
  imgOriginal = cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),1)
  landmarks = predictor(imgGray,face)
  myPoints=[]
  for n in range(68):
    x = landmarks.part(n).x
    y = landmarks.part(n).y
    myPoints.append([x,y])
    #cv2.circle(imgOriginal,(x,y),5,(50,50,255),cv2.FILLED)
    #cv2.putText(imgOriginal,str(n),(x,y-10),cv2.FONT_HERSHEY_COMPLEX_SMALL,0.8,(0,0,255),1)
  myPoints = np.array(myPoints)
  #nose points to select
  #nose_points = myPoints[27:35]

print(myPoints)
cv2_imshow(imgOriginal)
cv2.waitKey(0)


thanks in advance

CodePudding user response:

I think you need "Bulge" effects such as implode and explode. There are no implementation of these filters in OpenCV but, you can find other tools such as Wand(a python binding for ImageMagick) that have enter image description here

import numpy as np
import cv2
import math
import skimage.exposure

img = cv2.imread("portrait_of_mussorgsky2.jpg")

# set location and radius
cx = 130
cy = 109
radius = 30

# set distortion gain
gain = 1.5

# crop image 
crop = img[cy-radius:cy radius, cx-radius:cx radius]

# get dimensions
ht, wd = crop.shape[:2]
xcent = wd / 2
ycent = ht / 2
rad = min(xcent,ycent)

# set up the x and y maps as float32
map_x = np.zeros((ht, wd), np.float32)
map_y = np.zeros((ht, wd), np.float32)
mask = np.zeros((ht, wd), np.uint8)

# create map with the spherize distortion formula --- arcsin(r)
# xcomp = arcsin(r)*x/r; ycomp = arsin(r)*y/r
for y in range(ht):
    Y = (y - ycent)/ycent
    for x in range(wd):
        X = (x - xcent)/xcent
        R = math.hypot(X,Y)
        if R == 0:
            map_x[y, x] = x
            map_y[y, x] = y
            mask[y,x] = 255
        elif R >= .90:    # avoid extreme blurring near R = 1
            map_x[y, x] = x
            map_y[y, x] = y
            mask[y,x] = 0
        elif gain >= 0:
            map_x[y, x] = xcent*X*math.pow((2/math.pi)*(math.asin(R)/R), gain)   xcent
            map_y[y, x] = ycent*Y*math.pow((2/math.pi)*(math.asin(R)/R), gain)   ycent
            mask[y,x] = 255
        elif gain < 0:
            gain2 = -gain
            map_x[y, x] = xcent*X*math.pow((math.sin(math.pi*R/2)/R), gain2)   xcent
            map_y[y, x] = ycent*Y*math.pow((math.sin(math.pi*R/2)/R), gain2)   ycent
            mask[y,x] = 255

# remap using map_x and map_y
bump = cv2.remap(crop, map_x, map_y, cv2.INTER_LINEAR, borderMode = cv2.BORDER_CONSTANT, borderValue=(0,0,0))

# antialias edge of mask
# (pad so blur does not extend to edges of image, then crop later)
blur = 7
mask = cv2.copyMakeBorder(mask, blur,blur,blur,blur, borderType=cv2.BORDER_CONSTANT, value=(0))
mask = cv2.GaussianBlur(mask, (0,0), sigmaX=blur, sigmaY=blur, borderType = cv2.BORDER_DEFAULT)
h, w = mask.shape
mask = mask[blur:h-blur, blur:w-blur]
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
mask = skimage.exposure.rescale_intensity(mask, in_range=(127.5,255), out_range=(0,1))

# merge bump with crop using grayscale (not binary) mask
bumped = (bump * mask   crop * (1-mask)).clip(0,255).astype(np.uint8)

# insert bumped image into original
result = img.copy()
result[cy-radius:cy radius, cx-radius:cx radius] = bumped

# save results
cv2.imwrite("portrait_of_mussorgsky2_bump.jpg", result)

# display images
cv2.imshow('img', img)
cv2.imshow('crop', crop)
cv2.imshow('bump', bump)
cv2.imshow('mask', mask)
cv2.imshow('bumped', bumped)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Resulting Image:

enter image description here

  • Related