def join_parts(body_without_hands,hands) : body=cv2.imread(body_without_hands) arms1=cv2.imread(hands) body_prt=body.copy() body_prt[body.shape[0]/3:body.shape[0],:,:]=255 cv2.imwrite('body_prt.jpg',body_prt) prt_cnt=encContour('body_prt.jpg') leftindex=index_flatside(prt_cnt,"left") print "bodyleft",prt_cnt[leftindex,0] rightindex=index_flatside(prt_cnt,"right") print "bodyright",prt_cnt blank=np.zeros(arms1.shape,dtype=np.uint8) blank[:,:,:]=255 arms2=arms1.copy() #print arms1.shape[1] arms1[:,arms1.shape[1]/2:arms1.shape[1]]=blank[:,arms1.shape[1]/2:arms1.shape[1]] arms2[:,0 : arms1.shape[1]/2]=blank[:,0 : arms1.shape[1]/2] cv2.imwrite('arms1.jpg',arms1) cv2.imwrite('arms2.jpg',arms2) armsc1=encContour('arms1.jpg') armsc2=encContour('arms2.jpg') arm1index=index_flatside(armsc1,"right") arm2index=index_flatside(armsc2,"left") print "left top",armsc1[arm1index[1],0],'\nright top',armsc2[arm2index[1],0],"\nleftbody",prt_cnt[leftindex[1],0],"\nrightbody",prt_cnt[rightindex[1],0] armsc1[:,0]=armsc1[:,0]-armsc1[arm1index[0],0]+prt_cnt[leftindex[0],0] armsc2[:,0]=armsc2[:,0]-armsc2[arm2index[0],0]+prt_cnt[rightindex[0],0] cv2.drawContours(blank,[armsc1,armsc2,encContour(body_without_hands)],-1,(0),1) plt.imshow(blank) plt.show()
def giveHeights(ref, height, args): rext = give_extpoints(encContour(ref)) scaling = float(rext["bottom"][1] - rext["top"][1]) / height for i in args: print i xext = give_extpoints(encContour(i)) print "Height of " + i + " is :", float(xext["bottom"][1] - xext["top"][1]) / scaling
def side_fit(test, reference): ''' Under process Inputs: test: side view of test image (without head) reference: side view of reference image (without head) Output: test: cropped and resized test image reference: final reference image having same height as the test image ''' test_contour = encContour(test) reference_contour = encContour(reference) test = (np.zeros((test.shape[0],test.shape[1],3), np.uint8)).copy() reference = (np.zeros((reference.shape[0],reference.shape[1],3), np.uint8)).copy() cv2.drawContours(test, test_contour, -1 ,(255,255,255),1) cv2.drawContours(reference, reference_contour, -1 ,(255,255,255),1) t_l=tuple(test_contour[test_contour[:,:,0].argmin()][0]) t_r=tuple(test_contour[test_contour[:,:,0].argmax()][0]) t_t=tuple(test_contour[test_contour[:,:,1].argmin()][0]) t_b=tuple(test_contour[test_contour[:,:,1].argmax()][0]) error = (t_b[1] - t_t[1]) / 100 test = test[t_t[1] + error :t_b[1], t_l[0]:t_r[0]] r_l=tuple(reference_contour[reference_contour[:,:,0].argmin()][0]) r_r=tuple(reference_contour[reference_contour[:,:,0].argmax()][0]) r_t=tuple(reference_contour[reference_contour[:,:,1].argmin()][0]) r_b=tuple(reference_contour[reference_contour[:,:,1].argmax()][0]) error = (r_b[1] - r_t[1]) / 100 reference = reference[r_t[1] + error :r_b[1], r_l[0]:r_r[0]] height = 700 test = cvt_small(test, height) reference = cvt_small(reference, height) ''' Now divide body in 13 parts Reference : http://www.drawinghowtodraw.com/drawing-lessons/drawing-faces-lessons/cc-figure-drawing-anatomy-caricatures.html ''' scaling = horizontal_scale(test[0:(t_b[1] - t_t[1])*1/13, 0: r_b[1] - r_t[1]], reference[0:(t_b[1] - t_t[1])*1/13, 0: r_b[1] - r_t[1]]) reference = cv2.resize(reference,(int(round(reference.shape[1]*scaling)), height)) ''' cv2.imshow('reference',reference) cv2.imshow('test',test) cv2.waitKey(0) ''' return test, reference
def resize_image(imgf,imgs): f=encContour(imgf) s=encContour(imgs) ft=tuple(f[f[:,:,1].argmin()][0]) fb=tuple(f[f[:,:,1].argmax()][0]) st=tuple(s[s[:,:,1].argmin()][0]) sb=tuple(s[s[:,:,1].argmax()][0]) fh=fb[1]-ft[1] sh=sb[1]-st[1] ratio=fh/sh s[:,0,1]=(s[:,0,1]-st[1])*ratio+st[1] cv2.drawContours(imgs,s,0,(255),2) return imgf,imgs
def ratioheight(mfront,mfrontf,mside,rimg) : m=encContour(mfront) mf=encContour(mfrontf) ms=encContour(mside) r=encContour(rimg) mp=give_extpoints(m) mfp=give_extpoints(mf) msp=give_extpoints(ms) rp=give_extpoints(r) heightm=mp['bottom'][1]-mp['top'][1] heightmf=mfp['bottom'][1]-mfp['top'][1] heightms=msp['bottom'][1]-msp['top'][1] heightr=rp['bottom'][1]-rp['top'][1] ratios=[float(heightm)/heightr,float(heightmf)/heightr,float(heightms)/heightr] return ratios
def resize_model(mfront, mfrontf, mside, rimg): ''' inputs: mfront: front view of reference model mfrontf: front view of reference model arms stretched mside: side view of reference model rimg: test image ( front or side ) ''' mf = encContour(mfront) ms = encContour(mside) mff = encContour(mfrontf) rimg = encContour(rimg) et_mf = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mf = tuple(mf[mf[:, :, 1].argmax()][0]) et_ms = tuple(ms[ms[:, :, 1].argmin()][0]) eb_ms = tuple(ms[ms[:, :, 1].argmax()][0]) et_mff = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mff = tuple(mff[mff[:, :, 1].argmax()][0]) et_rimg = tuple(rimg[rimg[:, :, 1].argmin()][0]) eb_rimg = tuple(rimg[rimg[:, :, 1].argmax()][0]) hf = eb_mf[1] - et_mf[1] hs = eb_ms[1] - et_ms[1] hff = eb_mff[1] - et_mff[1] hrimg = eb_rimg[1] - et_rimg[1] ratios = [float(hf) / hrimg, float(hs) / hrimg, float(hff) / hrimg] f = cv2.imread(mfront) s = cv2.imread(mside) ff = cv2.imread(mfrontf) #print f.shape,s.shape #exit(0) floating1 = float(f.shape[1]) / ratios[0] floating2 = float(f.shape[0]) / ratios[0] f1 = cv2.resize(f, (int(floating1), int(floating2)), interpolation=cv2.INTER_CUBIC) floating1 = float(s.shape[1]) / ratios[1] floating2 = float(s.shape[0]) / ratios[1] s1 = cv2.resize(s, (int(floating1), int(floating2)), interpolation=cv2.INTER_CUBIC) floating1 = float(ff.shape[1]) / ratios[0] floating2 = float(ff.shape[0]) / ratios[0] ff1 = cv2.resize(ff, (int(floating1), int(floating2)), interpolation=cv2.INTER_CUBIC) cv2.imwrite('mfront.jpg', f1) cv2.imwrite('mside.jpg', s1) cv2.imwrite('mfrontf.jpg', ff1) return 'mfront.jpg', 'mside.jpg', 'mfrontf.jpg'
def resize_model(mfront,mfrontf,mside,rimg): mf=encContour(mfront) ms=encContour(mside) mff=encContour(mfrontf) rimgc=encContour(rimg) et_mf = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mf = tuple(mf[mf[:, :, 1].argmax()][0]) et_ms = tuple(ms[ms[:, :, 1].argmin()][0]) eb_ms = tuple(ms[ms[:, :, 1].argmax()][0]) et_mff = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mff = tuple(mff[mff[:, :, 1].argmax()][0]) et_rimg = tuple(rimgc[rimgc[:, :, 1].argmin()][0]) eb_rimg = tuple(rimgc[rimgc[:, :, 1].argmax()][0]) hf=eb_mf[1]-et_mf[1] hs=eb_ms[1]-et_ms[1] hff=eb_mff[1]-et_mff[1] hrimg=eb_rimg[1]-et_rimg[1] ratios=[float(hf)/hrimg,float(hs)/hrimg,float(hff)/hrimg] f=mfront.copy() s=(mside.copy()) ff=(mfrontf.copy()) #print f.shape,s.shape #exit(0) floating1=float(f.shape[1])/ratios[0] floating2=float(f.shape[0])/ratios[0] f1=cv2.resize(f,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) floating1=float(s.shape[1])/ratios[1] floating2=float(s.shape[0])/ratios[1] s1=cv2.resize(s,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) floating1=float(ff.shape[1])/ratios[2] floating2=float(ff.shape[0])/ratios[2] ff1=cv2.resize(ff,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) resized_mfront = f1.copy() resized_mfrontf = ff1.copy() resized_mside = s1.copy() #cv2.imwrite('mfront.jpg',f1) #cv2.imwrite('mside.jpg',s1) #cv2.imwrite('mfrontf.jpg',ff1) ratios=ratioheight(resized_mfront, resized_mfrontf, resized_mside, rimg) for i in ratios : if i < 0.93 or i >1.07 : return resize_model(resized_mfront, resized_mfrontf, resized_mside, rimg) return resized_mfront , resized_mfrontf , resized_mside
def resize_model(mfront,mfrontf,mside,rimg): ''' inputs: mfront: front view of reference model mfrontf: front view of reference model arms stretched mside: side view of reference model rimg: test image ( front or side ) ''' mf=encContour(mfront) ms=encContour(mside) mff=encContour(mfrontf) rimg=encContour(rimg) et_mf = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mf = tuple(mf[mf[:, :, 1].argmax()][0]) et_ms = tuple(ms[ms[:, :, 1].argmin()][0]) eb_ms = tuple(ms[ms[:, :, 1].argmax()][0]) et_mff = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mff = tuple(mff[mff[:, :, 1].argmax()][0]) et_rimg = tuple(rimg[rimg[:, :, 1].argmin()][0]) eb_rimg = tuple(rimg[rimg[:, :, 1].argmax()][0]) hf=eb_mf[1]-et_mf[1] hs=eb_ms[1]-et_ms[1] hff=eb_mff[1]-et_mff[1] hrimg=eb_rimg[1]-et_rimg[1] ratios=[float(hf)/hrimg,float(hs)/hrimg,float(hff)/hrimg] f=cv2.imread(mfront) s=cv2.imread(mside) ff=cv2.imread(mfrontf) #print f.shape,s.shape #exit(0) floating1=float(f.shape[1])/ratios[0] floating2=float(f.shape[0])/ratios[0] f1=cv2.resize(f,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) floating1=float(s.shape[1])/ratios[1] floating2=float(s.shape[0])/ratios[1] s1=cv2.resize(s,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) floating1=float(ff.shape[1])/ratios[0] floating2=float(ff.shape[0])/ratios[0] ff1=cv2.resize(ff,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) cv2.imwrite('mfront.jpg',f1) cv2.imwrite('mside.jpg',s1) cv2.imwrite('mfrontf.jpg',ff1) return 'mfront.jpg','mside.jpg','mfrontf.jpg'
def resize_model(mfront,mfrontf,mside,rimg): mf=encContour(mfront) ms=encContour(mside) mff=encContour(mfrontf) rimgc=encContour(rimg) et_mf = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mf = tuple(mf[mf[:, :, 1].argmax()][0]) et_ms = tuple(ms[ms[:, :, 1].argmin()][0]) eb_ms = tuple(ms[ms[:, :, 1].argmax()][0]) et_mff = tuple(mf[mf[:, :, 1].argmin()][0]) eb_mff = tuple(mff[mff[:, :, 1].argmax()][0]) et_rimg = tuple(rimgc[rimgc[:, :, 1].argmin()][0]) eb_rimg = tuple(rimgc[rimgc[:, :, 1].argmax()][0]) hf=eb_mf[1]-et_mf[1] hs=eb_ms[1]-et_ms[1] hff=eb_mff[1]-et_mff[1] hrimg=eb_rimg[1]-et_rimg[1] ratios=[float(hf)/hrimg,float(hs)/hrimg,float(hff)/hrimg] f=cv2.imread(mfront) s=cv2.imread(mside) ff=cv2.imread(mfrontf) #print f.shape,s.shape #exit(0) floating1=float(f.shape[1])/ratios[0] floating2=float(f.shape[0])/ratios[0] f1=cv2.resize(f,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) floating1=float(s.shape[1])/ratios[1] floating2=float(s.shape[0])/ratios[1] s1=cv2.resize(s,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) floating1=float(ff.shape[1])/ratios[2] floating2=float(ff.shape[0])/ratios[2] ff1=cv2.resize(ff,(int(floating1),int(floating2)),interpolation=cv2.INTER_CUBIC) cv2.imwrite('mfront.jpg',f1) cv2.imwrite('mside.jpg',s1) cv2.imwrite('mfrontf.jpg',ff1) ratios=ratioheight('mfront.jpg','mfrontf.jpg','mside.jpg',rimg) for i in ratios : if i < 0.93 or i >1.07 : return resize_model('mfront.jpg','mfrontf.jpg','mside.jpg',rimg) return 'mfront.jpg','mside.jpg','mfrontf.jpg'
def join_parts(body_without_hands, hands): body = cv2.imread(body_without_hands) arms1 = cv2.imread(hands) body_prt = body.copy() body_prt[body.shape[0] / 3:body.shape[0], :, :] = 255 cv2.imwrite('body_prt.jpg', body_prt) prt_cnt = encContour('body_prt.jpg') leftindex = index_flatside(prt_cnt, "left") print "bodyleft", prt_cnt[leftindex, 0] rightindex = index_flatside(prt_cnt, "right") print "bodyright", prt_cnt blank = np.zeros(arms1.shape, dtype=np.uint8) blank[:, :, :] = 255 arms2 = arms1.copy() #print arms1.shape[1] arms1[:, arms1.shape[1] / 2:arms1.shape[1]] = blank[:, arms1.shape[1] / 2:arms1.shape[1]] arms2[:, 0:arms1.shape[1] / 2] = blank[:, 0:arms1.shape[1] / 2] cv2.imwrite('arms1.jpg', arms1) cv2.imwrite('arms2.jpg', arms2) armsc1 = encContour('arms1.jpg') armsc2 = encContour('arms2.jpg') arm1index = index_flatside(armsc1, "right") arm2index = index_flatside(armsc2, "left") print "left top", armsc1[arm1index[1], 0], '\nright top', armsc2[ arm2index[1], 0], "\nleftbody", prt_cnt[leftindex[1], 0], "\nrightbody", prt_cnt[rightindex[1], 0] armsc1[:, 0] = armsc1[:, 0] - armsc1[arm1index[0], 0] + prt_cnt[leftindex[0], 0] armsc2[:, 0] = armsc2[:, 0] - armsc2[arm2index[0], 0] + prt_cnt[rightindex[0], 0] cv2.drawContours( blank, [armsc1, armsc2, encContour(body_without_hands)], -1, (0), 1) plt.imshow(blank) plt.show()
def plot3dHuman2 (front,arms,side,points): #Extracting enclosing contours s=encContour(side) f=encContour(front) arms1=cv2.imread(arms) blank=np.zeros(arms1.shape,dtype=np.uint8) blank[:,:,:]=255 arms2=cv2.imread(arms) # imposing blank ( white) image on half right ( and then left ) of the image having both the arms. arms1[:,arms1.shape[1]/2:arms1.shape[1]]=blank[:,arms1.shape[1]/2:arms1.shape[1]] arms2[:,0 : arms1.shape[1]/2]=blank[:,0 : arms1.shape[1]/2] # Now arms1 and arms2 have only the left and the right arm respectively armsc1=encContour(arms1) armsc2=encContour(arms2) extarms1=give_extindices(armsc1)["right"] extarms2=give_extindices(armsc2)["left"] #index_flatside gives the extreme indices on the specified side of the contour with slight deviation # index1 and index2 will store only the coordinates with lower y component ( i.e. topmost point ) index1=index_flatside(armsc1,"right")[0] index2=index_flatside(armsc2,"left")[0] extf=give_extpoints(f) exts=give_extpoints(s) f1=f.copy() s1=s.copy() # removing that extra bracket f1=f1[:,0] s1=s1[:,0] #Refer the file index_flatside for its usage bindex1=index_flatside(f,"left",0.33) bindex2=index_flatside(f,"right",0.33) matplotlib.rcParams['legend.fontsize']=10 fig=plt.figure() ax=fig.gca(projection='3d') # Changing reference (matplotlib places origin in the usual mathamatical way (not at the topmost left )) f1=f1[:]-[(extf["left"][0]+extf["right"][0])/2,extf["bottom"][1]] f1[:,1]=-f1[:,1] s1=s1[:]-[(exts["left"][0]+exts["right"][0])/2,exts["bottom"][1]] s1[:,1]=-s1[:,1] armsc1[:,0,1]=-armsc1[:,0,1] armsc2[:,0,1]=-armsc2[:,0,1] if len(points) != 0 : points[0][1]=-(points[0][1]-extf["bottom"][1]) points[1][1]=-(points[1][1]-extf["bottom"][1]) #Matching the topmost shoulder point ( bringing the arms to the main body ) armsc1[:,0]=armsc1[:,0]-armsc1[index1,0]+f1[bindex1[0]] armsc2[:,0]=armsc2[:,0]-armsc2[index2,0]+f1[bindex2[0]] #Defining the vertical range of arms using the main body (The real image may not be symmetric,one arm may be slightly higher than the other ) armsVrange=[min(f1[bindex1[1]][1],f1[bindex2[1]][1]),max(f1[bindex1[0]][1],f1[bindex2[0]][1])] # again removing that extra bracket for the arms armsc1=armsc1[:,0] armsc2=armsc2[:,0] extarms1=give_extpoints2(armsc1) extarms2=give_extpoints2(armsc2) # To get the lateral position of arms, matching the midpoint of side view at that point with the midpoint of arm temp=retval((f1[bindex1[0],1]+f1[bindex1[1],1])/2,s1) yl,yr=[0,0] if len(temp)==2: yl=(temp[0]+temp[1])/2 temp=retval((f1[bindex2[0],1]+f1[bindex2[1],1])/2,s1) if len(temp)==2 : yr=(temp[0]+temp[1])/2 # X,Y,Z stored the final plotting points in 3d X=[] ;Y=[];Z=[] # armsplotting decides whether to plot both the arms or not ( part plotting of arms or even plotting one arm is not supported ) armsplotting = False if len(points)!=0 : clickrange=[points[0][1],points[0][0]] clickrange=sorted(clickrange,reverse=False) for p in clickrange : if p <armsVrange[1] and p > armsVrange[0] : print " Don't click on arms " exit(1) if clickrange[0]<armsVrange[0] and clickrange[1] > armsVrange[1] : armsplotting=True else : armsplotting =True if armsplotting : #Plotting arms theta=np.linspace(0,2*np.pi,15) iterator=np.linspace(extarms1["right"][0],extarms1["left"][0],50) for i in iterator : x1=retval2(i,armsc1) if len(x1)!=2 : continue x_a=(x1[0]+x1[1])/2 radius = (abs(x1[1]-x1[0]))/2 for th in theta : X.append(i) Y.append(yl+radius*np.cos(th)) Z.append(x_a+radius*np.sin(th)) iterator=np.linspace(extarms2["right"][0],extarms2["left"][0],50) for i in iterator : x1=retval2(i,armsc2) if len(x1)!=2 : continue x_a=(x1[0]+x1[1])/2 radius = abs(x1[1]-x1[0])/2 for th in theta : X.append(i) Y.append(yr+radius*np.cos(th)) Z.append(x_a+radius*np.sin(th)) points=np.array(points) theta=np.linspace(0,2*np.pi,30) if len(points) != 0 : t= float( points[1,1]- points[0,1])/(max(max(f1[:,1]),max(s1[:,1]))*0.01) iterator=np.linspace(points[0,1],points[1,1],abs(t)) else : iterator=np.linspace(0,max(max(f1[:,1]),max(s1[:,1])),100) #For every value in iterator plotting the data points considering ellipses for fl in iterator: x1=retval(fl,f1) y1=retval(fl,s1) if len(y1) !=2 : continue if len(x1)%2 !=0 or len(x1) >5 or len(x1) == 0: continue x_a=(x1[0]+x1[1])/2 y_a=(y1[0]+y1[1])/2 a=(x1[1]-x1[0])/2 b=(y1[1]-y1[0])/2 for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) if len(x1) ==4 : x_a=(x1[2]+x1[3])/2 y_a=(y1[0]+y1[1])/2 a=(x1[2]-x1[3])/2 b=(y1[1]-y1[0])/2 for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) fig=plt.figure() ax=fig.add_subplot(111,projection='3d') #Using wireframe plots ( please improve this : spaces between legs is not clear, may use surface plots ) ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10) # Create cubic bounding box to simulate equal aspect ratio max_range = np.array([max(X)-min(X), max(Y)-min(Y),max(Z)-min(Z)]).max() Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(max(X)+min(X)) Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(max(Y)+min(Y)) Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(max(Z)+min(Z)) for xb, yb, zb in zip(Xb, Yb, Zb): ax.plot([xb], [yb], [zb], 'w') plt.show()
def ret_hscale(rimg, mimg, args): # getting the enclosing contours of real image and model image realc = encContour(rimg) modelc = encContour(mimg) extreal = give_extpoints(realc) extmodel = give_extpoints(modelc) midreal = int(0.25 * extreal["bottom"][1] + 0.75 * extreal["top"][1]) midmodel = int(0.25 * extmodel["bottom"][1] + 0.75 * extmodel["top"][1]) realindex = give_index_ct(realc, midreal + extreal["top"][1]) modelindex = give_index_ct(modelc, midmodel + extmodel["top"][1]) min = realc[realindex[0], 0, 0] # for real leftreal = realindex[0] k = 0 for i in realindex: k += 1 for i in range(1, len(realindex)): if min > realc[realindex[i], 0, 0]: leftreal = realindex[i] min = realc[realindex[i], 0, 0] min = modelc[modelindex[0], 0, 0] # for model leftmodel = modelindex[0] k = 0 for i in modelindex: k += 1 for i in range(1, len(modelindex)): if min > modelc[modelindex[i], 0, 0]: leftmodel = modelindex[i] min = modelc[modelindex[i], 0, 0] #figuring out the direction on which to move to reach the shoulder directionreal = '+' if realc[leftreal + 1, 0, 1] + realc[ leftreal + 2, 0, 1] + realc[leftreal + 3, 0, 1] < realc[ leftreal, 0, 1] + realc[leftreal - 1, 0, 1] + realc[leftreal - 2, 0, 1] else '-' directionmodel = '+' if modelc[leftmodel + 1, 0, 1] + modelc[ leftmodel + 2, 0, 1] + modelc[leftmodel + 3, 0, 1] < modelc[ leftmodel, 0, 1] + modelc[leftmodel - 1, 0, 1] + modelc[leftmodel - 2, 0, 1] else '-' # Getting the shoulder point for real image i = leftreal avr, avm = [0, 0] a = realc[i, 0] while (1): if directionreal == '+': b = realc[i + 1, 0] c = realc[i + 2, 0] d = realc[i + 3, 0] e = realc[i + 4, 0] f = realc[i + 5, 0] avr = [ float(b[0] + c[0] + d[0] + e[0] + f[0]) / 5, float(b[1] + c[1] + d[1] + e[1] + f[1]) / 5 ] i += 5 else: b = realc[i - 1, 0] c = realc[i - 2, 0] d = realc[i - 3, 0] e = realc[i - 4, 0] f = realc[i - 5, 0] avr = [ float(b[0] + c[0] + d[0] + e[0] + f[0]) / 5, float(b[1] + c[1] + d[1] + e[1] + f[1]) / 5 ] i -= 5 if avr[0] != a[0] and abs(float(avr[1] - a[1]) / (avr[0] - a[0])) < 1: realshoulder = a break a = avr # Getting the shoulder point for model image i = leftmodel a = modelc[i, 0] while (1): if directionreal == '+': b = modelc[i + 1, 0] c = modelc[i + 2, 0] d = modelc[i + 3, 0] e = modelc[i + 4, 0] f = modelc[i + 5, 0] avm = [ float(b[0] + c[0] + d[0] + e[0] + f[0]) / 5, float(b[1] + c[1] + d[1] + e[1] + f[1]) / 5 ] i += 5 else: b = modelc[i - 1, 0] c = modelc[i - 2, 0] d = modelc[i - 3, 0] e = modelc[i - 4, 0] f = modelc[i - 5, 0] avm = [ float(b[0] + c[0] + d[0] + e[0] + f[0]) / 5, float(b[1] + c[1] + d[1] + e[1] + f[1]) / 5 ] i -= 5 if avm[0] != a[0] and abs(float(avm[1] - a[1]) / (avm[0] - a[0])) < 1: modelshoulder = a break a = avm # print realshoulder,modelshoulder #Calculating the width of model shoulder and real shoulder a = give_index_ct(realc, realshoulder[1]) print "realshoulder", realc[a[0], 0], realc[a[1], 0] b = give_index_ct(modelc, modelshoulder[1]) print "modelshoulder", modelc[b[0], 0], modelc[b[1], 0] if len(a) % 2 != 0 or len(b) % 2 != 0: print "some error message" exit(1) scaling = float(realc[a[1], 0, 0] - realc[a[0], 0, 0]) / ( modelc[b[1], 0, 0] - modelc[b[0], 0, 0]) print scaling for i in args: x = cv2.imread(i) # print x.shape,int(x.shape[1]*scaling) x1 = cv2.resize(x, (int(float(x.shape[1]) * scaling), x.shape[0]), interpolation=cv2.INTER_CUBIC) cv2.imwrite('scaled_' + i, x1) return scaling
def plot3dHuman(front, side, points): #Extracting enclosing contours s = encContour(side) f = encContour(front) # Function which gives x values for a height t and modified contour f def retval(t, f): x = [] for i in range(0, len(f) - 1): a = f[:, 1][i] - t b = f[:, 1][i + 1] - t if a * b == 0: if a == 0: x.append(f[:, 0][i]) else: x.append(f[:, 0][i + 1]) else: if a * b < 0: x.append((f[:, 0][i] + f[:, 0][i + 1]) / 2) sorted(x) # At this stage x has the required values but there may be some repeated values y = [] for i in range(0, len(x) - 1): if x[i] == x[i + 1]: continue y.append(x[i]) if len(x) is not 0: y.append(x[-1]) return y # Getting extreme points fl = tuple(f[f[:, :, 0].argmin()][0]) fr = tuple(f[f[:, :, 0].argmax()][0]) sl = tuple(s[s[:, :, 0].argmin()][0]) sr = tuple(s[s[:, :, 0].argmax()][0]) ft = tuple(f[f[:, :, 1].argmin()][0]) fb = tuple(f[f[:, :, 1].argmax()][0]) st = tuple(s[s[:, :, 1].argmin()][0]) sb = tuple(s[s[:, :, 1].argmax()][0]) #from height import height #height('man_front.jpg') #print ft,fb #print st,sb # manipulating the contour to set the lowest point to origin and the body in positive direction f1 = f f1 = f1[:, 0] - [fr[0] - (fl[0] + fr[0]) / 2, fb[1]] if len(points) != 0: points[:, 1] = points[:, 1] - fb[1] points[:, 1] = -points[:, 1] f1[:, 1] = -f1[:, 1] s1 = s s1 = s1[:, 0] - [sl[0] + (sl[0] + sr[0]) / 2, sb[1]] s1[:, 1] = -s1[:, 1] matplotlib.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') s1 #y=np.zeros(len(f1[:,0]),dtype=np.int64) #ax.plot(f1[:,0],y,f1[:,1],label='front') #x=np.zeros(len(s1[:,0]),dtype=np.int64) #ax.plot(x,s1[:,0],s1[:,1],label='side') ax.set_aspect('equal') #print retval(800,f1) #Using 30 data points per height theta = np.linspace(0, 2 * np.pi, 30) if len(points) != 0: t = float(points[1, 1] - points[0, 1]) / (max(max(f1[:, 1]), max(s1[:, 1])) * 0.01) iterator = np.linspace(points[0, 1], points[1, 1], t) else: iterator = np.linspace(0, max(max(f1[:, 1]), max(s1[:, 1])), 100) X = [] Y = [] Z = [] #For every value in iterator plotting the data points considering ellipses for fl in iterator: x1 = retval(fl, f1) y1 = retval(fl, s1) if len(x1) % 2 != 0: continue if len(x1) == 6: x_a = (x1[0] + x1[1]) / 2 y_a = (y1[0] + y1[1]) / 2 x_a1 = (x1[4] + x1[5]) / 2 a = (x1[1] - x1[0]) / 2 a1 = (x1[5] - x1[4]) / 2 b = a b1 = a1 for temp in (x1[0] + x1[1]) / 2 + a * np.cos(theta): X.append(temp) for temp in (y1[0] + y1[1]) / 2 + b * np.sin(theta): Y.append(temp) Z.append(fl) for temp in (x1[4] + x1[5]) / 2 + a1 * np.cos(theta): X.append(temp) for temp in (y1[0] + y1[1]) / 2 + b1 * np.sin(theta): Y.append(temp) Z.append(fl) xf = [] if len(x1) == 6: xf = [x1[2], x1[3]] elif len(x1) in [2, 4]: xf = x1 else: maxdiff = 0 d = 0 index = -1 while d < len(x1): diff = x1[d + 1] - x1[d] if diff > maxdiff: maxdiff = diff index = d d += 2 xf = [x1[index], x1[index + 1]] try: if x1[index - 1] - x1[index - 2] < 1.20 * ( x1[index + 1] - x1[index]) and x1[index - 1] - x1[ index - 2] > 0.80 * (x1[index + 1] - x1[index]): xf = x1[index - 2:index + 2] x1 = xf elif x1[index + 3] - x1[index + 2] < 1.20 * ( x1[index + 1] - x1[index]) and x1[index + 3] - x1[ index + 2] > 0.80 * (x1[index + 1] - x1[index]): xf = x1[index:index + 4] x1 = xf except IndexError: pass # print len(x1),len(y1) if xf == []: continue x_a = (xf[1] + xf[0]) / 2 y_a = (y1[0] + y1[1]) / 2 a = (xf[1] - xf[0]) / 2 b = (y1[1] - y1[0]) / 2 #print y1,b for temp in x_a + a * np.cos(theta): X.append(temp) for temp in y_a + b * np.sin(theta): Y.append(temp) Z.append(fl) if (len(x1) == 4): a = (x1[3] - x1[2]) / 2 x_a = (x1[3] + x1[2]) / 2 y_a = (y1[0] + y1[1]) / 2 for temp in x_a + a * np.cos(theta): X.append(temp) for temp in y_a + b * np.sin(theta): Y.append(temp) Z.append(fl) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') #print len(X),len(Y),len(Z) #Using wireframe plots ( please improve this : spaces between legs is not clear, may use surface plots ) ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) # Create cubic bounding box to simulate equal aspect ratio max_range = np.array([max(X) - min(X), max(Y) - min(Y), max(Z) - min(Z)]).max() Xb = 0.5 * max_range * np.mgrid[ -1:2:2, -1:2:2, -1:2:2][0].flatten() + 0.5 * (max(X) + min(X)) Yb = 0.5 * max_range * np.mgrid[ -1:2:2, -1:2:2, -1:2:2][1].flatten() + 0.5 * (max(Y) + min(Y)) Zb = 0.5 * max_range * np.mgrid[ -1:2:2, -1:2:2, -1:2:2][2].flatten() + 0.5 * (max(Z) + min(Z)) for xb, yb, zb in zip(Xb, Yb, Zb): ax.plot([xb], [yb], [zb], 'w') plt.show()
def plot3dHuman2(front, arms, side, points): #Extracting enclosing contours s = encContour(side) f = encContour(front) #armsc=encContour(arms) # arms has both the arms so encContour would give the bigger contour but we want both the contours. Dividing the image into two and then using encContour for both the images arms1 = cv2.imread(arms) blank = np.zeros(arms1.shape, dtype=np.uint8) blank[:, :, :] = 255 arms2 = cv2.imread(arms) #print arms1.shape[1] arms1[:, arms1.shape[1] / 2:arms1.shape[1]] = blank[:, arms1.shape[1] / 2:arms1.shape[1]] arms2[:, 0:arms1.shape[1] / 2] = blank[:, 0:arms1.shape[1] / 2] armsc1 = encContour(arms1) armsc2 = encContour(arms2) extarms1 = give_extindices(armsc1)["right"] extarms2 = give_extindices(armsc2)["left"] index1 = index_flatside(armsc1, "right")[0] index2 = index_flatside(armsc2, "left")[0] # Getting extreme points ''' fl=tuple(f[f[:,:,0].argmin()][0]) fr=tuple(f[f[:,:,0].argmax()][0]) sl=tuple(s[s[:,:,0].argmin()][0]) sr=tuple(s[s[:,:,0].argmax()][0]) ft=tuple(f[f[:,:,1].argmin()][0]) fb=tuple(f[f[:,:,1].argmax()][0]) st=tuple(s[s[:,:,1].argmin()][0]) sb=tuple(s[s[:,:,1].argmax()][0]) ''' extf = give_extpoints(f) exts = give_extpoints(s) f1 = f.copy() s1 = s.copy() f1 = f1[:, 0] s1 = s1[:, 0] bindex1 = index_flatside(f, "left", 0.33) bindex2 = index_flatside(f, "right", 0.33) matplotlib.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') # Changing reference f1 = f1[:] - [(extf["left"][0] + extf["right"][0]) / 2, extf["bottom"][1]] f1[:, 1] = -f1[:, 1] s1 = s1[:] - [(exts["left"][0] + exts["right"][0]) / 2, exts["bottom"][1]] s1[:, 1] = -s1[:, 1] armsc1[:, 0, 1] = -armsc1[:, 0, 1] armsc2[:, 0, 1] = -armsc2[:, 0, 1] if len(points) != 0: #print points,fb[1] points[0][1] = -(points[0][1] - extf["bottom"][1]) points[1][1] = -(points[1][1] - extf["bottom"][1]) #print points #Matching the topmost shoulder point ( bringing the arms to the main body ) armsc1[:, 0] = armsc1[:, 0] - armsc1[index1, 0] + f1[bindex1[0]] armsc2[:, 0] = armsc2[:, 0] - armsc2[index2, 0] + f1[bindex2[0]] armsVrange = [ min(f1[bindex1[1]][1], f1[bindex2[1]][1]), max(f1[bindex1[0]][1], f1[bindex2[0]][1]) ] armsc1 = armsc1[:, 0] armsc2 = armsc2[:, 0] extarms1 = give_extpoints2(armsc1) extarms2 = give_extpoints2(armsc2) temp = retval((f1[bindex1[0], 1] + f1[bindex1[1], 1]) / 2, s1) yl, yr = [0, 0] if len(temp) == 2: yl = (temp[0] + temp[1]) / 2 temp = retval((f1[bindex2[0], 1] + f1[bindex2[1], 1]) / 2, s1) if len(temp) == 2: yr = (temp[0] + temp[1]) / 2 X = [] Y = [] Z = [] armsplotting = False clickrange = [points[0][1], points[0][0]] clickrange = sorted(clickrange, reverse=False) for p in clickrange: if p < armsVrange[1] and p > armsVrange[0]: print " Don't click on arms " exit(1) if clickrange[0] < armsVrange[0] and clickrange[1] > armsVrange[1]: armsplotting = True if armsplotting: #Plotting arms theta = np.linspace(0, 2 * np.pi, 15) iterator = np.linspace(extarms1["right"][0], extarms1["left"][0], 50) for i in iterator: x1 = retval2(i, armsc1) #print x1 if len(x1) != 2: continue x_a = (x1[0] + x1[1]) / 2 radius = (abs(x1[1] - x1[0])) / 2 for th in theta: X.append(i) Y.append(yl + radius * np.cos(th)) Z.append(x_a + radius * np.sin(th)) iterator = np.linspace(extarms2["right"][0], extarms2["left"][0], 50) for i in iterator: x1 = retval2(i, armsc2) if len(x1) != 2: #print x1 continue x_a = (x1[0] + x1[1]) / 2 radius = abs(x1[1] - x1[0]) / 2 for th in theta: X.append(i) Y.append(yr + radius * np.cos(th)) Z.append(x_a + radius * np.sin(th)) points = np.array(points) theta = np.linspace(0, 2 * np.pi, 30) if len(points) != 0: t = float(points[1, 1] - points[0, 1]) / (max(max(f1[:, 1]), max(s1[:, 1])) * 0.01) iterator = np.linspace(points[0, 1], points[1, 1], abs(t)) else: iterator = np.linspace(0, max(max(f1[:, 1]), max(s1[:, 1])), 100) #For every value in iterator plotting the data points considering ellipses for fl in iterator: x1 = retval(fl, f1) y1 = retval(fl, s1) if len(y1) != 2: continue if len(x1) % 2 != 0 or len(x1) > 5 or len(x1) == 0: continue x_a = (x1[0] + x1[1]) / 2 y_a = (y1[0] + y1[1]) / 2 a = (x1[1] - x1[0]) / 2 b = (y1[1] - y1[0]) / 2 for temp in x_a + a * np.cos(theta): X.append(temp) for temp in y_a + b * np.sin(theta): Y.append(temp) Z.append(fl) if len(x1) == 4: x_a = (x1[2] + x1[3]) / 2 y_a = (y1[0] + y1[1]) / 2 a = (x1[2] - x1[3]) / 2 b = (y1[1] - y1[0]) / 2 #print y1,b for temp in x_a + a * np.cos(theta): X.append(temp) for temp in y_a + b * np.sin(theta): Y.append(temp) Z.append(fl) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') #Using wireframe plots ( please improve this : spaces between legs is not clear, may use surface plots ) ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) # Create cubic bounding box to simulate equal aspect ratio max_range = np.array([max(X) - min(X), max(Y) - min(Y), max(Z) - min(Z)]).max() Xb = 0.5 * max_range * np.mgrid[ -1:2:2, -1:2:2, -1:2:2][0].flatten() + 0.5 * (max(X) + min(X)) Yb = 0.5 * max_range * np.mgrid[ -1:2:2, -1:2:2, -1:2:2][1].flatten() + 0.5 * (max(Y) + min(Y)) Zb = 0.5 * max_range * np.mgrid[ -1:2:2, -1:2:2, -1:2:2][2].flatten() + 0.5 * (max(Z) + min(Z)) for xb, yb, zb in zip(Xb, Yb, Zb): ax.plot([xb], [yb], [zb], 'w') plt.show()
def disint(inpf, inps): ''' inputs: inpf: input front image file inps: input side image file output: enclosing contour of: 1. head 2. body without head 3. side view of head 4. body without head side view 5. hands(front view) 6. body without hands(front view) ''' imgf=cv2.imread(inpf) imgs=cv2.imread(inps) cont_f=encContour(inpf) cont_s=encContour(inps) #getting extreme points l=tuple(cont_f[cont_f[:,:,0].argmin()][0]) r=tuple(cont_f[cont_f[:,:,0].argmax()][0]) t=tuple(cont_f[cont_f[:,:,1].argmin()][0]) b=tuple(cont_f[cont_f[:,:,1].argmax()][0]) ls=tuple(cont_s[cont_s[:,:,0].argmin()][0]) rs=tuple(cont_s[cont_s[:,:,0].argmax()][0]) ts=tuple(cont_s[cont_s[:,:,1].argmin()][0]) bs=tuple(cont_s[cont_s[:,:,1].argmax()][0]) length=r[0]-l[0] ran=np.linspace(0,b[1],200) for i in range(0,200): if i==0: continue ran[i]=b[1]-ran[i] c=0 z=0 X=[] for i in ran: x11=retval(i,cont_f,0) #0 for y-coordinate if z==1: break if len(x11)==2 and c==0: wl=x11[1]-x11[0] c=1 if len(x11)==2 and c==1: nl=x11[1]-x11[0] if nl<0.65*wl: X.append([x11[0],b[1]-i]) X.append([x11[1],b[1]-i]) z=1 break imgf1=imgf imgs1=imgs h,w,d=imgf.shape h2,w2,d2=imgs.shape img1=np.zeros((h,w,d), dtype=np.uint8) img1[:,:]=[255,255,255] img2=np.zeros((h,w,d), dtype=np.uint8) img2[:,:]=[255,255,255] img2[0:int(i),:]=imgf1[0:int(i),:] #projecting the head in blank image imgf1[0:int(i),:]=img1[0:int(i),:] #removing the head from copy of image cv2.imwrite('head.jpg', img2) cv2.imwrite('body_without_head.jpg', imgf1) dist=int(i-t[1]) img5=np.zeros((h2,w2,d2), dtype=np.uint8) img5[:,:]=[255,255,255] img6=np.zeros((h2,w2,d2), dtype=np.uint8) img6[:,:]=[255,255,255] img6[0:ts[1]+dist,:]=imgs1[0:ts[1]+dist,:] #projecting the head in blank image imgs1[0:ts[1]+dist,:]=img5[0:ts[1]+dist,:] cv2.imwrite('head_side_view.jpg', img6) cv2.imwrite('side_view_without_head.jpg', imgs1) #cv2.imshow(img2, 'head') Y=[] height,width,dim=imgf.shape #for hands a=0 b=0 ran2=np.linspace(1,r[0],200) for i in ran2: yp=retval(i-1,cont_f,1) y=retval(i,cont_f,1) if len(yp)==2 and a==0: p_slope=y[0]-yp[0] a=1 #comparing slopes continue elif len(yp)==2 and len(y)==2 and a==1: #print len(yp) slope=y[1]-yp[1] if p_slope!=0: b=1 ratio=slope/p_slope p_slope=slope else: continue if b==1 and ratio>1.2: Y.append([i,y[1]]) Y.append([width-i,y[1]]) Y.append([i,y[0]]) Y.append([width-i,y[0]]) break #print Y imgf2=cv2.imread(inpf) img3=np.zeros((height,width,dim), dtype=np.uint8) img3[:,:]=[255,255,255] img4=np.zeros((height,width,dim), dtype=np.uint8) img4[:,:]=[255,255,255] img4[:,0:int(i)]=imgf2[:,0:int(i)] img4[:,int(width-i):int(width)]=imgf2[:,int(width-i):int(width)] #projecting hands in blank image imgf2[:,0:int(i)]=img3[:,0:int(i)] #removing hands from copy of original image imgf2[:,int(width-i):int(width)]=img3[:,int(width-i):int(width)] #also taking the mirror image of one hand cv2.imwrite('hands.jpg', img4) cv2.imwrite('body_without_hands.jpg', imgf2) img4=cv2.imread('hands.jpg') imgf2=cv2.imread('body_without_hands.jpg') #plt.imshow(img4) #plt.show() #plt.imshow(imgf2) #plt.show() return 'head.jpg', 'body_without_head.jpg', 'head_side_view.jpg', 'side_view_without_head.jpg', 'hands.jpg', 'body_without_hands.jpg';
def disint_hum(inpf): imgf=cv2.imread(inpf) #imgs=cv2.imread(inps) cont_f=encContour(inpf) #cont_s=encContour(inps) #getting extreme points l=tuple(cont_f[cont_f[:,:,0].argmin()][0]) r=tuple(cont_f[cont_f[:,:,0].argmax()][0]) t=tuple(cont_f[cont_f[:,:,1].argmin()][0]) b=tuple(cont_f[cont_f[:,:,1].argmax()][0]) #ls=tuple(cont_s[cont_s[:,:,0].argmin()][0]) #rs=tuple(cont_s[cont_s[:,:,0].argmax()][0]) #ts=tuple(cont_s[cont_s[:,:,1].argmin()][0]) #bs=tuple(cont_s[cont_s[:,:,1].argmax()][0]) hi=b[1]-t[1] p=t[1]+0.4*hi x=retval(p,cont_f,0) ran=np.linspace(t[1],p,200) for i in range(0,200): ran[i]=p-ran[i] ran[i]=ran[i]+t[1] Y=[] a=0 b=0 #sorted(ran,reverse=True) for i in ran: #i=-i #i=p+i if i==0: continue yp=retval(i-1,cont_f,0) y=retval(i,cont_f,0) if len(yp)==2 and a==0: p_slope=yp[0]-y[0] a=1 #comparing slopes continue elif len(yp)==2 and len(y)==2 and a==1: #print len(yp) slope=yp[0]-y[0] if p_slope!=0: b=1 ratio=float(slope)/p_slope p_slope=slope else: continue if b==1 and ratio>1.5: Y.append(y[0]) Y.append(y[1]) break imgf2=cv2.imread(inpf) height,width,dim=imgf2.shape img3=np.zeros((height,width,dim), dtype=np.uint8) img3[:,:]=[255,255,255] img4=np.zeros((height,width,dim), dtype=np.uint8) img4[:,:]=[255,255,255] img4[:,0:int(Y[0])]=imgf2[:,0:int(Y[0])] img4[:,int(Y[1]):int(width)]=imgf2[:,int(Y[1]):int(width)] #projecting hands in blank image imgf2[:,0:int(Y[0])]=img3[:,0:int(Y[0])] #removing hands from copy of original image imgf2[:,int(Y[1]):int(width)]=img3[:,int(Y[1]):int(width)] #also taking the mirror image of one hand cv2.imwrite('handsi.jpg', img4) #plt.imshow(img4) #plt.show() cv2.imwrite('body_without_handsi.jpg', imgf2) #plt.imshow(imgf2) #plt.show() #for cutting head ran2=np.linspace(l[0],r[0],300) q=0 Z=[] for i in range(0,300): if i==0: continue z=retval(ran2[i],cont_f,1) zp=retval(ran2[i-1],cont_f,1) if a==0: p_slope=z[0]-zp[0] a=1 elif a==1: slope=z[0]-zp[0] ratio=p_slope/slope p_slope=slope if ratio>1.2: Z.append(z[0]) w=ran2[i] break imgf2=cv2.imread(inpf) img1=np.zeros((height,width,dim), dtype=np.uint8) img1[:,:]=[255,255,255] img2=np.zeros((height,width,dim), dtype=np.uint8) img2[:,:]=[255,255,255] img2[0:int(Z[0]),:]=imgf[0:int(Z[0]),:] imgf[0:int(Z[0]),:]=img1[0:int(Z[0]),:] cv2.imwrite('headi.jpg', img2) #plt.imshow(img4) #plt.show() cv2.imwrite('body_without_headi.jpg', imgf) #plt.imshow(imgf2) #plt.show() return 'handsi.jpg','body_without_handsi.jpg','head.jpg','body_without_headi.jpg'
#from resize_model import resize_model import cv2 from plot3d import plot3dHuman2 from plot3d import plot3dHuman from ret2Val import ret2Val from resize_model import resize_model from ret_hscale import ret_hscale from outer import encContour from cvt_small import cvt_small ''' This is the test file of ret_hscale assuming that the real image is girl_front.jpg (as the hands down image of man.jpg was not available :P ) and model image is mfront.jpg ''' #resize_model('man_front.jpg','man_frontf.jpg','man_side.jpg','girl_front.jpg') #a=ret2Val('mfront.jpg') #ret_hscale('girl_front.jpg','mfront.jpg',('mfront.jpg','mfrontf.jpg')) #plot3dHuman2('body_without_hands.jpg','hands.jpg','man_side.jpg',[]) #from outer import encContour image = cv2.imread('x1.jpg') resized_image = cvt_small(image,800) #y=encContour('P_20160618_001210.jpg') contour_of_resized_image = encContour(resized_image) cv2.drawContours(resized_image, contour_of_resized_image, -1 ,(255),2) cv2.imshow('final',resized_image) cv2.waitKey(0)
def plot3dHuman2 (front,arms,side,points): #Extracting enclosing contours s=encContour(side) f=encContour(front) #armsc=encContour(arms) # arms has both the arms so encContour would give the bigger contour but we want both the contours. Dividing the image into two and then using encContour for both the images arms1=cv2.imread(arms) blank=np.zeros(arms1.shape,dtype=np.uint8) blank[:,:,:]=255 arms2=cv2.imread(arms) #print arms1.shape[1] arms1[:,arms1.shape[1]/2:arms1.shape[1]]=blank[:,arms1.shape[1]/2:arms1.shape[1]] arms2[:,0 : arms1.shape[1]/2]=blank[:,0 : arms1.shape[1]/2] # Uncomment this cv2.imwrite('arms1.jpg',arms1) cv2.imwrite('arms2.jpg',arms2) armsc1=encContour('arms1.jpg') armsc2=encContour('arms2.jpg') extarms1=give_extpoints(armsc1) extarms2=give_extpoints(armsc2) trpoint=extarms1["right"] tlpoint=extarms2["left"] trindex=give_index_ct(armsc1,trpoint[1]) tlindex=give_index_ct(armsc2,tlpoint[1]) ''' if len(trindex)!=2 or len(tlindex)!=2 : print "Debug....." exit(1) trindex=trindex[1] if armsc1[trindex[1],0,0]>armsc1[trindex[0],0,0] else trindex[0] tlindex=tlindex[0] if armsc1[tlindex[0],0,0]<armsc1[tlindex[1],0,0] else tlindex[1] ''' mini=tlindex[0] maxi=trindex[0] if len(trindex) >1: for temp in trindex : if armsc1[maxi,0,0] < armsc1[temp,0,0] : maxi=temp trindex=maxi if len(tlindex) >1 : for temp in tlindex : if armsc2[mini,0,0] > armsc2[temp,0,0] : mini=temp tlindex=mini if armsc1[trindex+1,0,1] < armsc1[trindex,0,1] : while armsc1[trindex+1,0,0] == armsc1[trindex,0,0] : trindex+=1 elif armsc1[trindex-1,0,1] < armsc1[trindex,0,1] : while armsc1[trindex-1,0,0] ==armsc1[trindex,0,0] : trindex-=1 else : print "Consider more pixels" exit(1) if armsc2[tlindex+1,0,1] < armsc2[tlindex,0,1] : while armsc2[tlindex+1,0,0] == armsc2[tlindex,0,0] : tlindex+=1 elif armsc2[tlindex-1,0,1] < armsc2[tlindex,0,1] : while armsc2[tlindex-1,0,0] ==armsc2[tlindex,0,0] : tlindex-=1 else : print "Consider more pixels" exit(1) ''' extfront=give_extpoints(f) x1=extfront["left"] x2=extfront["right"] armsVrange=[] index1=give_index_ct(f,x1[1]) index2=give_index_ct(f,x2[1]) if len(index1) != 2 or len(index2) !=2 : print "debug...." exit(1) indexl=index1[0] if f[index1[0],0,0]<f[index1[1],0,0] else index1[1] indexr=index2[0] if f[index2[0],0,0]>f[index2[1],0,0] else index2[1] g=False indexleft=[] indexright=[] for temp in [indexl,indexr] : t1=temp t2=temp i=False k=False while i==k : i=not i if f[t1+1,0,0]==f[t1,0,0] : if i is not k : k=i t1=t1+1 if f[t2-1,0,0]==f[t2,0,0] : if i is not k : k=i t2=t2 -1 if not g : if f[t1,0,0] > f[t2,0,0] : indexleft=[t2,t1] else : indexleft=[t1,t2] else : if f[t1,0,0] > f[t2,0,0] : indexright=[t2,t1] else : indexright=[t1,t2] g= not g ''' # Getting extreme points fl=tuple(f[f[:,:,0].argmin()][0]) fr=tuple(f[f[:,:,0].argmax()][0]) sl=tuple(s[s[:,:,0].argmin()][0]) sr=tuple(s[s[:,:,0].argmax()][0]) ft=tuple(f[f[:,:,1].argmin()][0]) fb=tuple(f[f[:,:,1].argmax()][0]) st=tuple(s[s[:,:,1].argmin()][0]) sb=tuple(s[s[:,:,1].argmax()][0]) #from height import height #height('man_front.jpg') #print ft,fb #print st,sb # manipulating the contour to set the lowest point to origin and the body in positive direction f1=f f1=f1[:,0]-[fr[0]-(fl[0]+fr[0])/2,fb[1]] if len(points) != 0 : print points,fb[1] points[:,1]=points[:,1]-fb[1] points[:,1]=-points[:,1] #print points[:,1] f1[:,1]=-f1[:,1] s1=s s1=s1[:,0]-[sl[0]+(sl[0]+sr[0])/2,sb[1]] s1[:,1]=-s1[:,1] matplotlib.rcParams['legend.fontsize']=10 fig=plt.figure() ax=fig.gca(projection='3d') s1 #y=np.zeros(len(f1[:,0]),dtype=np.int64) #ax.plot(f1[:,0],y,f1[:,1],label='front') #x=np.zeros(len(s1[:,0]),dtype=np.int64) #ax.plot(x,s1[:,0],s1[:,1],label='side') ax.set_aspect('equal') #print retval(800,f1) # .......................added!........................... extfront=give_extpoints2(f1) x1=extfront["left"] x2=extfront["right"] index1=give_index_ct2(f1,x1[1]) index2=give_index_ct2(f1,x2[1]) if len(index1) != 2 or len(index2) !=2 : print "debug...." exit(1) indexl=index1[0] if f1[index1[0],0]<f1[index1[1],0] else index1[1] indexr=index2[0] if f1[index2[0],0]>f1[index2[1],0] else index2[1] g=False indexleft=[] indexright=[] for temp in [indexl,indexr] : t1=temp t2=temp i=False k=False while i==k : i=not i if f1[t1+1,0]==f1[t1,0] : if i is not k : k=i t1=t1+1 if f1[t2-1,0]==f1[t2,0] : if i is not k : k=i t2=t2 -1 if not g : if f1[t1,1] < f1[t2,1] : indexleft=[t1,t2] else : indexleft=[t2,t1] else : if f1[t1,1] < f1[t2,1] : indexright=[t1,t2] else : indexright=[t2,t1] g= not g z_al=(f1[indexleft[0],1]+f1[indexleft[1],1])/2 temp=retval(z_al,s1) y_al=(temp[0]+temp[1])/2 z_ar=(f1[indexright[0],1]+f1[indexright[1],1])/2 temp=retval(z_ar,s1) y_ar=(temp[0]+temp[1])/2 #Matching the topmost shoulder point ( bringing the arms to the main body ) armsc1=armsc1[:,0]-armsc1[trindex,0]+f1[indexleft[1]] armsc2=armsc2[:,0]-armsc2[tlindex,0]+f1[indexright[1]] armsVrange=[min(f1[indexleft[0]][1],f1[indexright[0]][1]),max(f1[indexleft[1]][1],f1[indexright[1]][1])] extarms1=give_extpoints2(armsc1) extarms2=give_extpoints2(armsc2) X=[] ;Y=[];Z=[] armsplotting = True for p in points : if p[1] <armsVrange[1] and p[1] > armsVrange[0] : armsplotting=False print " Don't click on arms " if armsplotting : #Plotting arms theta=np.linspace(0,2*np.pi,15) iterator=np.linspace(extarms1["right"][0],extarms1["left"][0],20) for i in iterator : x1=retval2(i,armsc1) #print x1 if len(x1)!=2 : continue x_a=(x1[0]+x1[1])/2 radius = abs(x1[1]-x1[0]) for th in theta : X.append(i) Y.append(y_al+radius*np.cos(th)) Z.append(x_a+radius*np.sin(th)) iterator=np.linspace(extarms2["right"][0],extarms2["left"][0],20) for i in iterator : x1=retval2(i,armsc2) if len(x1)!=2 : #print x1 continue x_a=(x1[0]+x1[1])/2 radius = abs(x1[1]-x1[0]) #print radius for th in theta : X.append(i) Y.append(y_ar+radius*np.cos(th)) Z.append(x_a+radius*np.sin(th)) #Using 30 data points per height theta=np.linspace(0,2*np.pi,30) if len(points) != 0 : t= float( points[1,1]- points[0,1])/(max(max(f1[:,1]),max(s1[:,1]))*0.01) iterator=np.linspace(points[0,1],points[1,1],abs(t)) #iterator=[ int (k) for k in iterator ] # print points,points[0,1],points[1,1],iterator else : iterator=np.linspace(0,max(max(f1[:,1]),max(s1[:,1])),100) #For every value in iterator plotting the data points considering ellipses for fl in iterator: x1=retval(fl,f1) y1=retval(fl,s1) if len(y1) !=2 : continue if len(x1)%2 !=0 or len(x1) >5 or len(x1) == 0: continue x_a=(x1[0]+x1[1])/2 y_a=(y1[0]+y1[1])/2 a=(x1[1]-x1[0])/2 b=(y1[1]-y1[0])/2 #print a,b #print y1,b for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) if len(x1) ==4 : x_a=(x1[2]+x1[3])/2 y_a=(y1[0]+y1[1])/2 a=(x1[2]-x1[3])/2 b=(y1[1]-y1[0])/2 #print y1,b for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) fig=plt.figure() ax=fig.add_subplot(111,projection='3d') #print len(X),len(Y),len(Z) #Using wireframe plots ( please improve this : spaces between legs is not clear, may use surface plots ) ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10) # Create cubic bounding box to simulate equal aspect ratio max_range = np.array([max(X)-min(X), max(Y)-min(Y),max(Z)-min(Z)]).max() Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(max(X)+min(X)) Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(max(Y)+min(Y)) Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(max(Z)+min(Z)) for xb, yb, zb in zip(Xb, Yb, Zb): ax.plot([xb], [yb], [zb], 'w') plt.show()
def disint_hum(inpf): imgf = cv2.imread(inpf) #imgs=cv2.imread(inps) cont_f = encContour(inpf) #cont_s=encContour(inps) #getting extreme points l = tuple(cont_f[cont_f[:, :, 0].argmin()][0]) r = tuple(cont_f[cont_f[:, :, 0].argmax()][0]) t = tuple(cont_f[cont_f[:, :, 1].argmin()][0]) b = tuple(cont_f[cont_f[:, :, 1].argmax()][0]) #ls=tuple(cont_s[cont_s[:,:,0].argmin()][0]) #rs=tuple(cont_s[cont_s[:,:,0].argmax()][0]) #ts=tuple(cont_s[cont_s[:,:,1].argmin()][0]) #bs=tuple(cont_s[cont_s[:,:,1].argmax()][0]) hi = b[1] - t[1] p = t[1] + 0.4 * hi x = retval(p, cont_f, 0) ran = np.linspace(t[1], p, 200) for i in range(0, 200): ran[i] = p - ran[i] ran[i] = ran[i] + t[1] Y = [] a = 0 b = 0 #sorted(ran,reverse=True) for i in ran: #i=-i #i=p+i if i == 0: continue yp = retval(i - 1, cont_f, 0) y = retval(i, cont_f, 0) if len(yp) == 2 and a == 0: p_slope = yp[0] - y[0] a = 1 #comparing slopes continue elif len(yp) == 2 and len(y) == 2 and a == 1: #print len(yp) slope = yp[0] - y[0] if p_slope != 0: b = 1 ratio = float(slope) / p_slope p_slope = slope else: continue if b == 1 and ratio > 1.5: Y.append(y[0]) Y.append(y[1]) break imgf2 = cv2.imread(inpf) height, width, dim = imgf2.shape img3 = np.zeros((height, width, dim), dtype=np.uint8) img3[:, :] = [255, 255, 255] img4 = np.zeros((height, width, dim), dtype=np.uint8) img4[:, :] = [255, 255, 255] img4[:, 0:int(Y[0])] = imgf2[:, 0:int(Y[0])] img4[:, int(Y[1]):int(width)] = imgf2[:, int(Y[1]):int( width)] #projecting hands in blank image imgf2[:, 0:int(Y[0])] = img3[:, 0:int( Y[0])] #removing hands from copy of original image imgf2[:, int(Y[1]):int(width)] = img3[:, int(Y[1]):int( width)] #also taking the mirror image of one hand cv2.imwrite('handsi.jpg', img4) #plt.imshow(img4) #plt.show() cv2.imwrite('body_without_handsi.jpg', imgf2) #plt.imshow(imgf2) #plt.show() #for cutting head ran2 = np.linspace(l[0], r[0], 300) q = 0 Z = [] for i in range(0, 300): if i == 0: continue z = retval(ran2[i], cont_f, 1) zp = retval(ran2[i - 1], cont_f, 1) if a == 0: p_slope = z[0] - zp[0] a = 1 elif a == 1: slope = z[0] - zp[0] ratio = p_slope / slope p_slope = slope if ratio > 1.2: Z.append(z[0]) w = ran2[i] break imgf2 = cv2.imread(inpf) img1 = np.zeros((height, width, dim), dtype=np.uint8) img1[:, :] = [255, 255, 255] img2 = np.zeros((height, width, dim), dtype=np.uint8) img2[:, :] = [255, 255, 255] img2[0:int(Z[0]), :] = imgf[0:int(Z[0]), :] imgf[0:int(Z[0]), :] = img1[0:int(Z[0]), :] cv2.imwrite('headi.jpg', img2) #plt.imshow(img4) #plt.show() cv2.imwrite('body_without_headi.jpg', imgf) #plt.imshow(imgf2) #plt.show() return 'handsi.jpg', 'body_without_handsi.jpg', 'head.jpg', 'body_without_headi.jpg'
def disint(inpf, inps): ''' inputs: inpf: input front image file inps: input side image file output: enclosing contour of: 1. head 2. body without head 3. side view of head 4. body without head side view 5. hands(front view) 6. body without hands(front view) ''' imgf = cv2.imread(inpf) imgs = cv2.imread(inps) cont_f = encContour(inpf) cont_s = encContour(inps) #getting extreme points l = tuple(cont_f[cont_f[:, :, 0].argmin()][0]) r = tuple(cont_f[cont_f[:, :, 0].argmax()][0]) t = tuple(cont_f[cont_f[:, :, 1].argmin()][0]) b = tuple(cont_f[cont_f[:, :, 1].argmax()][0]) ls = tuple(cont_s[cont_s[:, :, 0].argmin()][0]) rs = tuple(cont_s[cont_s[:, :, 0].argmax()][0]) ts = tuple(cont_s[cont_s[:, :, 1].argmin()][0]) bs = tuple(cont_s[cont_s[:, :, 1].argmax()][0]) length = r[0] - l[0] ran = np.linspace(0, b[1], 200) for i in range(0, 200): if i == 0: continue ran[i] = b[1] - ran[i] c = 0 z = 0 X = [] for i in ran: x11 = retval(i, cont_f, 0) #0 for y-coordinate if z == 1: break if len(x11) == 2 and c == 0: wl = x11[1] - x11[0] c = 1 if len(x11) == 2 and c == 1: nl = x11[1] - x11[0] if nl < 0.65 * wl: X.append([x11[0], b[1] - i]) X.append([x11[1], b[1] - i]) z = 1 break imgf1 = imgf imgs1 = imgs h, w, d = imgf.shape h2, w2, d2 = imgs.shape img1 = np.zeros((h, w, d), dtype=np.uint8) img1[:, :] = [255, 255, 255] img2 = np.zeros((h, w, d), dtype=np.uint8) img2[:, :] = [255, 255, 255] img2[0:int(i), :] = imgf1[0:int(i), :] #projecting the head in blank image imgf1[0:int(i), :] = img1[0:int( i), :] #removing the head from copy of image cv2.imwrite('head.jpg', img2) cv2.imwrite('body_without_head.jpg', imgf1) dist = int(i - t[1]) img5 = np.zeros((h2, w2, d2), dtype=np.uint8) img5[:, :] = [255, 255, 255] img6 = np.zeros((h2, w2, d2), dtype=np.uint8) img6[:, :] = [255, 255, 255] img6[0:ts[1] + dist, :] = imgs1[0:ts[1] + dist, :] #projecting the head in blank image imgs1[0:ts[1] + dist, :] = img5[0:ts[1] + dist, :] cv2.imwrite('head_side_view.jpg', img6) cv2.imwrite('side_view_without_head.jpg', imgs1) #cv2.imshow(img2, 'head') Y = [] height, width, dim = imgf.shape #for hands a = 0 b = 0 ran2 = np.linspace(1, r[0], 200) for i in ran2: yp = retval(i - 1, cont_f, 1) y = retval(i, cont_f, 1) if len(yp) == 2 and a == 0: p_slope = y[0] - yp[0] a = 1 #comparing slopes continue elif len(yp) == 2 and len(y) == 2 and a == 1: #print len(yp) slope = y[1] - yp[1] if p_slope != 0: b = 1 ratio = slope / p_slope p_slope = slope else: continue if b == 1 and ratio > 1.2: Y.append([i, y[1]]) Y.append([width - i, y[1]]) Y.append([i, y[0]]) Y.append([width - i, y[0]]) break print Y imgf2 = cv2.imread(inpf) img3 = np.zeros((height, width, dim), dtype=np.uint8) img3[:, :] = [255, 255, 255] img4 = np.zeros((height, width, dim), dtype=np.uint8) img4[:, :] = [255, 255, 255] img4[:, 0:int(i)] = imgf2[:, 0:int(i)] img4[:, int(width - i):int(width)] = imgf2[:, int(width - i):int( width)] #projecting hands in blank image imgf2[:, 0:int(i)] = img3[:, 0:int( i)] #removing hands from copy of original image imgf2[:, int(width - i):int(width)] = img3[:, int(width - i):int( width)] #also taking the mirror image of one hand cv2.imwrite('hands.jpg', img4) cv2.imwrite('body_without_hands.jpg', imgf2) return 'head.jpg', 'body_without_head.jpg', 'head_side_view.jpg', 'side_view_without_head.jpg', 'hands.jpg', 'body_without_hands.jpg'
def plot3dHuman2 (front,arms,side,points): #Extracting enclosing contours s=encContour(side) f=encContour(front) #armsc=encContour(arms) # arms has both the arms so encContour would give the bigger contour but we want both the contours. Dividing the image into two and then using encContour for both the images arms1=cv2.imread(arms) blank=np.zeros(arms1.shape,dtype=np.uint8) blank[:,:,:]=255 arms2=cv2.imread(arms) #print arms1.shape[1] arms1[:,arms1.shape[1]/2:arms1.shape[1]]=blank[:,arms1.shape[1]/2:arms1.shape[1]] arms2[:,0 : arms1.shape[1]/2]=blank[:,0 : arms1.shape[1]/2] armsc1=encContour(arms1) armsc2=encContour(arms2) extarms1=give_extindices(armsc1)["right"] extarms2=give_extindices(armsc2)["left"] index1=index_flatside(armsc1,"right")[0] index2=index_flatside(armsc2,"left")[0] # Getting extreme points ''' fl=tuple(f[f[:,:,0].argmin()][0]) fr=tuple(f[f[:,:,0].argmax()][0]) sl=tuple(s[s[:,:,0].argmin()][0]) sr=tuple(s[s[:,:,0].argmax()][0]) ft=tuple(f[f[:,:,1].argmin()][0]) fb=tuple(f[f[:,:,1].argmax()][0]) st=tuple(s[s[:,:,1].argmin()][0]) sb=tuple(s[s[:,:,1].argmax()][0]) ''' extf=give_extpoints(f) exts=give_extpoints(s) f1=f.copy() s1=s.copy() f1=f1[:,0] s1=s1[:,0] bindex1=index_flatside(f,"left",0.33) bindex2=index_flatside(f,"right",0.33) matplotlib.rcParams['legend.fontsize']=10 fig=plt.figure() ax=fig.gca(projection='3d') # Changing reference f1=f1[:]-[(extf["left"][0]+extf["right"][0])/2,extf["bottom"][1]] f1[:,1]=-f1[:,1] s1=s1[:]-[(exts["left"][0]+exts["right"][0])/2,exts["bottom"][1]] s1[:,1]=-s1[:,1] armsc1[:,0,1]=-armsc1[:,0,1] armsc2[:,0,1]=-armsc2[:,0,1] if len(points) != 0 : #print points,fb[1] points[0][1]=-(points[0][1]-extf["bottom"][1]) points[1][1]=-(points[1][1]-extf["bottom"][1]) #print points #Matching the topmost shoulder point ( bringing the arms to the main body ) armsc1[:,0]=armsc1[:,0]-armsc1[index1,0]+f1[bindex1[0]] armsc2[:,0]=armsc2[:,0]-armsc2[index2,0]+f1[bindex2[0]] armsVrange=[min(f1[bindex1[1]][1],f1[bindex2[1]][1]),max(f1[bindex1[0]][1],f1[bindex2[0]][1])] armsc1=armsc1[:,0] armsc2=armsc2[:,0] extarms1=give_extpoints2(armsc1) extarms2=give_extpoints2(armsc2) temp=retval((f1[bindex1[0],1]+f1[bindex1[1],1])/2,s1) yl,yr=[0,0] if len(temp)==2: yl=(temp[0]+temp[1])/2 temp=retval((f1[bindex2[0],1]+f1[bindex2[1],1])/2,s1) if len(temp)==2 : yr=(temp[0]+temp[1])/2 X=[] ;Y=[];Z=[] armsplotting = False clickrange=[points[0][1],points[0][0]] clickrange=sorted(clickrange,reverse=False) for p in clickrange : if p <armsVrange[1] and p > armsVrange[0] : print " Don't click on arms " exit(1) if clickrange[0]<armsVrange[0] and clickrange[1] > armsVrange[1] : armsplotting=True if armsplotting : #Plotting arms theta=np.linspace(0,2*np.pi,15) iterator=np.linspace(extarms1["right"][0],extarms1["left"][0],50) for i in iterator : x1=retval2(i,armsc1) #print x1 if len(x1)!=2 : continue x_a=(x1[0]+x1[1])/2 radius = (abs(x1[1]-x1[0]))/2 for th in theta : X.append(i) Y.append(yl+radius*np.cos(th)) Z.append(x_a+radius*np.sin(th)) iterator=np.linspace(extarms2["right"][0],extarms2["left"][0],50) for i in iterator : x1=retval2(i,armsc2) if len(x1)!=2 : #print x1 continue x_a=(x1[0]+x1[1])/2 radius = abs(x1[1]-x1[0])/2 for th in theta : X.append(i) Y.append(yr+radius*np.cos(th)) Z.append(x_a+radius*np.sin(th)) points=np.array(points) theta=np.linspace(0,2*np.pi,30) if len(points) != 0 : t= float( points[1,1]- points[0,1])/(max(max(f1[:,1]),max(s1[:,1]))*0.01) iterator=np.linspace(points[0,1],points[1,1],abs(t)) else : iterator=np.linspace(0,max(max(f1[:,1]),max(s1[:,1])),100) #For every value in iterator plotting the data points considering ellipses for fl in iterator: x1=retval(fl,f1) y1=retval(fl,s1) if len(y1) !=2 : continue if len(x1)%2 !=0 or len(x1) >5 or len(x1) == 0: continue x_a=(x1[0]+x1[1])/2 y_a=(y1[0]+y1[1])/2 a=(x1[1]-x1[0])/2 b=(y1[1]-y1[0])/2 for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) if len(x1) ==4 : x_a=(x1[2]+x1[3])/2 y_a=(y1[0]+y1[1])/2 a=(x1[2]-x1[3])/2 b=(y1[1]-y1[0])/2 #print y1,b for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) fig=plt.figure() ax=fig.add_subplot(111,projection='3d') #Using wireframe plots ( please improve this : spaces between legs is not clear, may use surface plots ) ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10) # Create cubic bounding box to simulate equal aspect ratio max_range = np.array([max(X)-min(X), max(Y)-min(Y),max(Z)-min(Z)]).max() Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(max(X)+min(X)) Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(max(Y)+min(Y)) Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(max(Z)+min(Z)) for xb, yb, zb in zip(Xb, Yb, Zb): ax.plot([xb], [yb], [zb], 'w') plt.show()
from outer import encContour from height import height from plot import plot_contour from matplotlib import pyplot as plt import cv2 import numpy as np import copy import math import sys cnt1 = encContour('man_front.jpg') cnt2 = encContour('man_side.jpg') image = cv2.imread('man_front.jpg') plot_contour(cnt1=cnt1, cnt2=cnt2) #cv2.drawContours(image,cnt1,-1,(255),3) #cv2.imshow('front image',image) #cv2.waitKey(0) #print((cnt)) ''' #test for encContour image = cv2.imread(url) cnt = encContour(url) cv2.drawContours(image,cnt,-1,(255),3) cv2.imshow('image',image) cv2.waitKey(0) cv2.destroyAllWindows() '''
def plot3dHuman (front,side,points): #Extracting enclosing contours s=encContour(side) f=encContour(front) # Function which gives x values for a height t and modified contour f # Getting extreme points fl=tuple(f[f[:,:,0].argmin()][0]) fr=tuple(f[f[:,:,0].argmax()][0]) sl=tuple(s[s[:,:,0].argmin()][0]) sr=tuple(s[s[:,:,0].argmax()][0]) ft=tuple(f[f[:,:,1].argmin()][0]) fb=tuple(f[f[:,:,1].argmax()][0]) st=tuple(s[s[:,:,1].argmin()][0]) sb=tuple(s[s[:,:,1].argmax()][0]) #from height import height #height('man_front.jpg') #print ft,fb #print st,sb # manipulating the contour to set the lowest point to origin and the body in positive direction f1=f f1=f1[:,0]-[fr[0]-(fl[0]+fr[0])/2,fb[1]] if len(points) != 0 : points[:,1]=points[:,1]-fb[1] points[:,1]=-points[:,1] f1[:,1]=-f1[:,1] s1=s s1=s1[:,0]-[sl[0]+(sl[0]+sr[0])/2,sb[1]] s1[:,1]=-s1[:,1] matplotlib.rcParams['legend.fontsize']=10 fig=plt.figure() ax=fig.gca(projection='3d') s1 #y=np.zeros(len(f1[:,0]),dtype=np.int64) #ax.plot(f1[:,0],y,f1[:,1],label='front') #x=np.zeros(len(s1[:,0]),dtype=np.int64) #ax.plot(x,s1[:,0],s1[:,1],label='side') ax.set_aspect('equal') #print retval(800,f1) #Using 30 data points per height theta=np.linspace(0,2*np.pi,30) if len(points) != 0 : t= float( points[1,1]- points[0,1])/(max(max(f1[:,1]),max(s1[:,1]))*0.01) iterator=np.linspace(points[0,1],points[1,1],t) else : iterator=np.linspace(0,max(max(f1[:,1]),max(s1[:,1])),100) X=[] ;Y=[];Z=[] #For every value in iterator plotting the data points considering ellipses for fl in iterator: x1=retval(fl,f1) y1=retval(fl,s1) if len(x1)%2 !=0 : continue if len(x1) == 6: x_a=(x1[0]+x1[1])/2 y_a=(y1[0]+y1[1])/2 x_a1=(x1[4]+x1[5])/2 a=(x1[1]-x1[0])/2 a1=(x1[5]-x1[4])/2 b=a b1=a1 for temp in (x1[0]+x1[1])/2+a*np.cos(theta) : X.append(temp) for temp in (y1[0]+y1[1])/2+b*np.sin(theta) : Y.append(temp) Z.append(fl) for temp in (x1[4]+x1[5])/2+a1*np.cos(theta) : X.append(temp) for temp in (y1[0]+y1[1])/2+b1*np.sin(theta) : Y.append(temp) Z.append(fl) xf=[] if len(x1) == 6 : xf=[x1[2],x1[3]] elif len(x1) in [2,4] : xf=x1 else: maxdiff=0 d=0 index=-1 while d<len(x1) : diff=x1[d+1]-x1[d] if diff> maxdiff : maxdiff = diff index=d d+=2 xf=[x1[index],x1[index+1]] try : if x1[index-1]-x1[index-2] < 1.20*(x1[index+1]-x1[index]) and x1[index-1]-x1[index-2] > 0.80*(x1[index+1]-x1[index]) : xf=x1[index-2:index+2] x1=xf elif x1[index+3]-x1[index+2] < 1.20*(x1[index+1]-x1[index]) and x1[index+3]-x1[index+2] > 0.80*(x1[index+1]-x1[index]) : xf=x1[index:index+4] x1=xf except IndexError: pass # print len(x1),len(y1) if xf == [] : continue x_a=(xf[1]+xf[0])/2 y_a=(y1[0]+y1[1])/2 a=(xf[1]-xf[0])/2 b=(y1[1]-y1[0])/2 #print y1,b for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) if(len(x1)==4): a=(x1[3]-x1[2])/2 x_a=(x1[3]+x1[2])/2 y_a=(y1[0]+y1[1])/2 for temp in x_a+a*np.cos(theta) : X.append(temp) for temp in y_a+b*np.sin(theta) : Y.append(temp) Z.append(fl) fig=plt.figure() ax=fig.add_subplot(111,projection='3d') #Using wireframe plots ( please improve this : spaces between legs is not clear, may use surface plots ) ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10) # Create cubic bounding box to simulate equal aspect ratio ( this is just to keep same aspect ratio ) max_range = np.array([max(X)-min(X), max(Y)-min(Y),max(Z)-min(Z)]).max() Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(max(X)+min(X)) Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(max(Y)+min(Y)) Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(max(Z)+min(Z)) for xb, yb, zb in zip(Xb, Yb, Zb): ax.plot([xb], [yb], [zb], 'w') plt.show()
from outer import encContour from height import height from plot import plot_contour from matplotlib import pyplot as plt import cv2 import numpy as np import copy import math import sys cnt1 = encContour('man_front.jpg') cnt2 = encContour('man_side.jpg') image = cv2.imread('man_front.jpg') plot_contour(cnt1= cnt1, cnt2= cnt2) #cv2.drawContours(image,cnt1,-1,(255),3) #cv2.imshow('front image',image) #cv2.waitKey(0) #print((cnt)) ''' #test for encContour image = cv2.imread(url) cnt = encContour(url) cv2.drawContours(image,cnt,-1,(255),3) cv2.imshow('image',image) cv2.waitKey(0) cv2.destroyAllWindows() '''
def parts_divide_hum(inpf,inps): imgf= inpf.copy() imgs= inps.copy() cont_f=encContour(imgf) cont_s=encContour(inps) #getting extreme points l=tuple(cont_f[cont_f[:,:,0].argmin()][0]) r=tuple(cont_f[cont_f[:,:,0].argmax()][0]) t=tuple(cont_f[cont_f[:,:,1].argmin()][0]) b=tuple(cont_f[cont_f[:,:,1].argmax()][0]) ls=tuple(cont_s[cont_s[:,:,0].argmin()][0]) rs=tuple(cont_s[cont_s[:,:,0].argmax()][0]) ts=tuple(cont_s[cont_s[:,:,1].argmin()][0]) bs=tuple(cont_s[cont_s[:,:,1].argmax()][0]) hi=b[1]-t[1] p=t[1]+0.4*hi x=retval(p,cont_f,0) ran=np.linspace(t[1],p,200) for i in range(0,200): ran[i]=p-ran[i] ran[i]=ran[i]+t[1] Y=[] a=0 b=0 #sorted(ran,reverse=True) for i in range(1,200): #i=-i #i=p+i yp=retval(ran[i-1],cont_f,0) y=retval(ran[i],cont_f,0) if len(yp)==2 and a==0: p_slope=yp[0]-y[0] a=1 #comparing slopes continue elif len(yp)==2 and len(y)==2 and a==1: #print len(yp) slope=yp[0]-y[0] print p_slope if p_slope!=0: b=1 ratio=float(slope)/p_slope #print ratio p_slope=slope else: continue if b==1 and ratio>1.2: Y.append(y[0]) Y.append(y[1]) break imgf2 = inpf.copy() #imgf2=cv2.imread(inpf) height,width,dim=imgf2.shape img3=np.zeros((height,width,dim), dtype=np.uint8) img3[:,:]=[255,255,255] img4=np.zeros((height,width,dim), dtype=np.uint8) img4[:,:]=[255,255,255] img4[:,0:int(Y[0])]=imgf2[:,0:int(Y[0])] img4[:,int(Y[1]):int(width)]=imgf2[:,int(Y[1]):int(width)] #projecting hands in blank image imgf2[:,0:int(Y[0])]=img3[:,0:int(Y[0])] #removing hands from copy of original image imgf2[:,int(Y[1]):int(width)]=img3[:,int(Y[1]):int(width)] #also taking the mirror image of one hand cv2.imwrite('handsi.jpg', img4) #plt.imshow(img4) #plt.show() cv2.imwrite('body_without_handsi.jpg', imgf2) #plt.imshow(imgf2) #plt.show() #for cutting head wid2=(r[0]-l[0])/4 ran2=np.linspace(l[0]+wid2,r[0],300) q=0 Z=[] for i in range(0,300): if i==0: continue z=retval(ran2[i],cont_f,1) zp=retval(ran2[i-1],cont_f,1) if a==0: p_slope=float(zp[0]-z[0])/(ran2[i]-ran2[i-1]) a=1 elif a==1: slope=float(zp[0]-z[0])/(ran2[i]-ran2[i-1]) if p_slope!=0: ratio=float(slope)/p_slope #print ratio p_slope=slope if ratio>1.5: Z.append(zp[0]) w=ran2[i] break #print len(Z) #imgf2=cv2.imread(inpf) imgf2 = inpf.copy() img1=np.zeros((height,width,dim), dtype=np.uint8) img1[:,:]=[255,255,255] img2=np.zeros((height,width,dim), dtype=np.uint8) img2[:,:]=[255,255,255] img2[0:int(Z[0]),:]=imgf[0:int(Z[0]),:] imgf[0:int(Z[0]),:]=img1[0:int(Z[0]),:] cv2.imwrite('headi.jpg', img2) #plt.imshow(img4) #plt.show() cv2.imwrite('body_without_headi.jpg', imgf) #plt.imshow(imgf2) #plt.show() #head side view cont_h=encContour(img2) th=tuple(cont_h[cont_h[:,:,1].argmin()][0]) bh=tuple(cont_h[cont_h[:,:,1].argmax()][0]) head_len=bh[1]-th[1] m=ts[1]+head_len imgs2=imgs.copy() hs,ws,ds=imgs2.shape img5=np.zeros((hs,ws,ds), dtype=np.uint8) img5[:,:]=[255,255,255] img6=np.zeros((hs,ws,ds), dtype=np.uint8) img6[:,:]=[255,255,255] img6[0:int(m),:]=imgs2[0:int(m),:] imgs2[0:int(m),:]=img5[0:int(m),:] cv2.imwrite('head_sidei.jpg', img6) #plt.imshow(img4) #plt.show() cv2.imwrite('body_without_head_sidei.jpg', imgs2) #plt.imshow(imgf2) #plt.show() return 'headi.jpg','body_without_headi.jpg','head_sidei.jpg','body_without_head_sidei.jpg', 'handsi.jpg','body_without_handsi.jpg',
def ret_hscale(rimg,mimg,args): # getting the enclosing contours of real image and model image realc=encContour(rimg) modelc=encContour(mimg) extreal=give_extpoints(realc) extmodel=give_extpoints(modelc) midreal=int( 0.25*extreal["bottom"][1]+0.75*extreal["top"][1]) midmodel=int( 0.25*extmodel["bottom"][1]+0.75*extmodel["top"][1]) realindex=give_index_ct(realc,midreal+extreal["top"][1]) modelindex=give_index_ct(modelc,midmodel+extmodel["top"][1]) min=realc[realindex[0],0,0] # for real leftreal=realindex[0] k=0 for i in realindex : k+=1 for i in range(1,len(realindex)): if min> realc[realindex[i],0,0] : leftreal=realindex[i] min=realc[realindex[i],0,0] min=modelc[modelindex[0],0,0] # for model leftmodel=modelindex[0] k=0 for i in modelindex : k+=1 for i in range(1,len(modelindex)) : if min>modelc[modelindex[i],0,0] : leftmodel=modelindex[i] min=modelc[modelindex[i],0,0] #figuring out the direction on which to move to reach the shoulder directionreal='+' if realc[leftreal+1,0,1]+realc[leftreal+2,0,1]+realc[leftreal+3,0,1]<realc[leftreal,0,1]+realc[leftreal-1,0,1]+realc[leftreal-2,0,1] else '-' directionmodel='+' if modelc[leftmodel+1,0,1]+modelc[leftmodel+2,0,1]+modelc[leftmodel+3,0,1]<modelc[leftmodel,0,1]+modelc[leftmodel-1,0,1]+modelc[leftmodel-2,0,1] else '-' # Getting the shoulder point for real image i=leftreal avr,avm=[0,0] a=realc[i,0] while(1) : if directionreal=='+' : b=realc[i+1,0] c=realc[i+2,0] d=realc[i+3,0] e=realc[i+4,0] f=realc[i+5,0] avr=[float(b[0]+c[0]+d[0]+e[0]+f[0])/5,float(b[1]+c[1]+d[1]+e[1]+f[1])/5] i+=5 else: b=realc[i-1,0] c=realc[i-2,0] d=realc[i-3,0] e=realc[i-4,0] f=realc[i-5,0] avr=[float(b[0]+c[0]+d[0]+e[0]+f[0])/5,float(b[1]+c[1]+d[1]+e[1]+f[1])/5] i-=5 if avr[0]!=a[0] and abs(float(avr[1]-a[1])/(avr[0]-a[0]))<1 : realshoulder=a break a=avr # Getting the shoulder point for model image i=leftmodel a=modelc[i,0] while(1) : if directionreal=='+' : b=modelc[i+1,0] c=modelc[i+2,0] d=modelc[i+3,0] e=modelc[i+4,0] f=modelc[i+5,0] avm=[float(b[0]+c[0]+d[0]+e[0]+f[0])/5,float(b[1]+c[1]+d[1]+e[1]+f[1])/5] i+=5 else: b=modelc[i-1,0] c=modelc[i-2,0] d=modelc[i-3,0] e=modelc[i-4,0] f=modelc[i-5,0] avm=[float(b[0]+c[0]+d[0]+e[0]+f[0])/5,float(b[1]+c[1]+d[1]+e[1]+f[1])/5] i-=5 if avm[0]!=a[0] and abs(float(avm[1]-a[1])/(avm[0]-a[0]))<1 : modelshoulder=a break a=avm # print realshoulder,modelshoulder #Calculating the width of model shoulder and real shoulder a=give_index_ct(realc,realshoulder[1]) print "realshoulder",realc[a[0],0],realc[a[1],0] b=give_index_ct(modelc,modelshoulder[1]) print "modelshoulder",modelc[b[0],0],modelc[b[1],0] if len(a)%2!=0 or len(b)%2!=0 : print "some error message" exit(1) scaling=float(realc[a[1],0,0]-realc[a[0],0,0])/(modelc[b[1],0,0]-modelc[b[0],0,0]) print scaling for i in args : x=cv2.imread(i) # print x.shape,int(x.shape[1]*scaling) x1=cv2.resize(x,(int(float(x.shape[1])*scaling),x.shape[0]),interpolation=cv2.INTER_CUBIC) cv2.imwrite('scaled_'+i,x1) return scaling