Как запустить Python скрипт в Matlab? - PullRequest
0 голосов
/ 24 апреля 2020

У меня есть код, написанный на python jupyter notebook версии 3.6. Но моя другая работа в Matlab, и этот код python является небольшой частью этого. Итак, как мне запустить / вызвать скрипт python из MATLAB? Кроме того, я прилагаю код, написанный на python, который в основном выполняет некоторую обработку изображений. PS - Я пытался использовать помощь MATLAB, но я не мог понять это.

Заранее спасибо.

enter code hereimport cv2
import numpy as np 

img=cv2.imread('C:/Users/makar/Desktop/Thesis/images_with_different_background/WY04.jpg')
image = cv2.resize(img,(609,609))

def colorDectection(image, lower, upper,color):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    #print('hi')
    mask = cv2.inRange(hsv, lower, upper)
    #imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    (contours,_) = cv2.findContours(mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    for contour in contours: 
            area = cv2.contourArea(contour)
            if((area > 12000) or (area > 2000)):
                x,y,w,h = cv2.boundingRect(contour)
                centerCoord = (x+(w/2), y+(h/2))
                A=int(x+(w/2))
                B=int(y+(h/2))
                image = cv2.rectangle(image, (x-10,y-10),(x+w,y+h),(0,0,255),1)
                #cv2.putText(image,color,(x,y),cv2.FONT_HERSHEY_SIMPLEX,2.5,(0,255,255),3)
                cv2.putText(image, color, (A,B),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
                cv2.imshow("tracking", image)                  
                return color



list2=[]
lower_yellow = np.array([20, 30, 30])
upper_yellow = np.array([40, 255, 255])
value1=colorDectection(image=image , lower = lower_yellow, upper = upper_yellow,color="Yellow")
list2.append(value1)

lower_blue = np.array([95,150,0])
upper_blue = np.array([110, 255, 255])
value2=colorDectection(image=image , lower = lower_blue, upper = upper_blue,color="Blue")
list2.append(value2)
lower_green = np.array([40,20,20])
upper_green = np.array([60, 255, 255])
value3=colorDectection(image=image , lower = lower_green, upper = upper_green,color="Green")
list2.append(value3)
lower_pink = np.array([130,70,80])
upper_pink = np.array([210, 240, 240])
value5=colorDectection(image=image, lower = lower_pink, upper = upper_pink,color="Pink")
list2.append(value5)

k = cv2.waitKey(20000) & 0XFF 


list3=[x for x in list2 if x is not None]
print(list3)
if len(list3)==0:
    print('bottle is not detected')
elif list3[0]=='Blue' and len(list3)==1:
    print('bottle1')
elif list3[0]=='Yellow' and len(list3)==1:
    print('bottle2')
elif list3[0]=='Green' and len(list3)==1:
    print('bottle3')
elif list3[0]=='Pink' and len(list3)==1:
    print('bottle4')
elif list3[0]=='Light Blue' and len(list3)==1:
    print('bottle5')
elif list3[0]=='Yellow'and list3[1]=='Blue' and len(list3)==2:
    print('bottle6')
elif list3[0]=='Yellow' and list3[1]=='Green' and len(list3)==2:
    print('bottle7')
elif list3[0]=='Yellow' and list3[1]=='Pink' and len(list3)==2:
    print('bottle8')
elif list3[0]=='Blue' and list3[1]=='Green' and len(list3)==2:
    print('bottle9')
elif list3[0]=='Blue' and list3[1]=='Pink' and len(list3)==2:
    print('bottle10')
elif list3[0]=='Green' and list3[1]=='Pink' and len(list3)==2:
    print('bottle11') 


cv2.destroyAllWindows() 
...