how to convert python code to matlab?
5 views (last 30 days)
Show older comments
I have python code but i want matlab code for the same please help me to convert that code
here is the python code for the same..can u convert it to matlab?
""" Main code of Automated Defence System It will have two modes - Automatic and Manual Team no P34 """
import Tkinter import pythoncom, pyHook import serial import tkMessageBox import cv import Tkinter as tk from PIL import ImageTk, Image import subprocess import os import sys #ser=serial.Serial('COM10',9600) # Open Serial port COM13 and communicate at a baud rate of 9600 count=0 # Initializing the varaible count which controls the rotation of motor count1=0 start=Tkinter.Tk() # This is the main GUI Window
def automatic(): # The function for automatic mode. print 'automatic' class Target: def init(self): self.capture = cv.CaptureFromCAM(0) # Initialize the webcam
cv.NamedWindow("Target", 1) # Create a window to display the webcam feed
cv.NamedWindow("Track",1)
def run(self):
#cap=cv.CaptureFromCAM(1)
start.destroy()
frame = cv.QueryFrame(self.capture) # Capture the first frame
frame_size = cv.GetSize(frame) #Get the size of the first frame
grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3)
difference = None
while True:
# Capture frame from webcam
#target=cv.QueryFrame(cap)
#cv.ShowImage("Track",target)
color_image = cv.QueryFrame(self.capture)
# Smooth to get rid of false positives
cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)
if not difference:
# Initialize
difference = cv.CloneImage(color_image)
temp = cv.CloneImage(color_image)
cv.ConvertScale(color_image, moving_average, 1.0, 0.0)
else:
cv.RunningAvg(color_image, moving_average, 0.020, None)
# Convert the scale of the moving average.
cv.ConvertScale(moving_average, temp, 1.0, 0.0)
# Minus the current frame from the moving average.
cv.AbsDiff(color_image, temp, difference)
# Convert the image to grayscale.
cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)
cv.ShowImage("Target",color_image)
# Convert the image to black and white.
cv.Threshold(grey_image, grey_image,50, 255, cv.CV_THRESH_BINARY)
#smooth and threshold again to remove sparkles
cv.Smooth(grey_image,grey_image,cv.CV_GAUSSIAN,19,0)
cv.Threshold(grey_image,grey_image,240,255,cv.CV_THRESH_BINARY)
# Dilate and erode to get object blobs
cv.Dilate(grey_image, grey_image, None, 18)
cv.Erode(grey_image, grey_image, None, 10)
# Calculate movements
storage = cv.CreateMemStorage(0)
contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
points = []
while contour:
# Draw rectangles
bound_rect = cv.BoundingRect(list(contour))
contour = contour.h_next()
pt1 = (bound_rect[0], bound_rect[1])
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
points.append(pt1)
points.append(pt2)
cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1)
num_points = len(points)
if num_points:
# Draw bullseye in midpoint of all movements
x = y = 0
for point in points:
x += point[0]
y += point[1]
x /= num_points
y /= num_points
#xtemp=150-(.28*x)
#xint=int(float(xtemp))
#ytemp=150-(.28*y)
#yint=int(float(ytemp))
center_point = (x, y)
print center_point
cv.Circle(color_image, center_point, 40, cv.CV_RGB(255, 255, 255), 1)
cv.Circle(color_image, center_point, 30, cv.CV_RGB(255, 100, 0), 1)
cv.Circle(color_image, center_point, 20, cv.CV_RGB(255, 255, 255), 1)
cv.Circle(color_image, center_point, 10, cv.CV_RGB(255, 100, 0), 5)
if (x>=0 and x < 13 )and (y>=0 and y < 13):
ser.write(120)
ser.write(',')
ser.write(90)
ser.write('/')
if (x>=13 and x < 26) and (y>=13 and y<26):
ser.write(117)
ser.write(',')
ser.write(89)
ser.write('/')
if (x>=26 and x < 39) and (y>=26 and y<39):
ser.write(112)
ser.write(',')
ser.write(88)
ser.write('/')
if (x>=39 and x < 52) and( y>=39 and y<52):
ser.write(110)
ser.write(',')
ser.write(87)
ser.write('/')
if (x>=52 and x < 65 )and( y>=52 and y<65):
ser.write(107)
ser.write(',')
ser.write(86)
ser.write('/')
if (x>=65 and x <78 )and (y>=65 and y<78):
ser.write(103)
ser.write(',')
ser.write(85)
ser.write('/')
if (x>=78 and x <91 )and (y>=78 and y<91):
ser.write(100)
ser.write(',')
ser.write(84)
ser.write('/')
if (x>=91 and x < 104) and (y>=91 and y<104):
ser.write(98)
ser.write(',')
ser.write(83)
ser.write('/')
if (x>=104 and x <117 )and (y>=104 and y<117):
ser.write(97)
ser.write(',')
ser.write(82)
ser.write('/')
if (x>=117 and x <130) and (y>=117 and y<130):
ser.write(96)
ser.write(',')
ser.write(81)
ser.write('/')
if (x>=130 and x <143) and (y>=130 and y<143):
ser.write(95)
ser.write(',')
ser.write(80)
ser.write('/')
if (x>=143 and x <156 )and (y>=143 and y<156):
ser.write(94)
ser.write(',')
ser.write(79)
ser.write('/')
if (x>=156 and x <169 )and (y>=156 and y<169):
ser.write(93)
ser.write(',')
ser.write(78)
ser.write('/')
if (x>=169 and x <182 )and (y>=169 and y<182):
ser.write(92)
ser.write(',')
ser.write(75)
ser.write('/')
if (x>=182 and x <195) and (y>=182 and y<195):
ser.write(91)
ser.write(',')
ser.write(73)
ser.write('/')
if (x>=195 and x <208) and (y>=195 and y<208):
ser.write(90)
ser.write(',')
ser.write(70)
ser.write('/')
if (x>=208 and x <221) and (y>=208 and y<221):
ser.write(89)
ser.write(',')
ser.write(69)
ser.write('/')
if (x>=221 and x <234) and (y>=221 and y<234):
ser.write(88)
ser.write(',')
ser.write(68)
ser.write('/')
if (x>=234 and x <247) and (y>=234 and y<247):
ser.write(87)
ser.write(',')
ser.write(67)
ser.write('/')
if (x>=247 and x <260) and (y>=247 and y<260):
ser.write(86)
ser.write(',')
ser.write(66)
ser.write('/')
if (x>=260 and x <273) and (y>=260 and y<273):
ser.write(85)
ser.write(',')
ser.write(65)
ser.write('/')
if (x>=273 and x <286) and (y>=273 and y<286):
ser.write(84)
ser.write(',')
ser.write(64)
ser.write('/')
if (x>=286 and x <299) and (y>=286 and y<299):
ser.write(83)
ser.write(',')
ser.write(63)
ser.write('/')
if (x>=299 and x <312) and (y>=299 and y<312):
ser.write(82)
ser.write(',')
ser.write(62)
ser.write('/')
if (x>=312 and x <325) and (y>=312 and y<325):
ser.write(81)
ser.write(',')
ser.write(61)
ser.write('/')
if (x>=325 and x <338) and (y>=325 and y<338):
ser.write(80)
ser.write(',')
ser.write(60)
ser.write('/')
if (x>=338 and x <351) and (y>=338 and y<351) :
ser.write(79)
ser.write(',')
ser.write(59)
ser.write('/')
if (x>=351 and x <364) and (y>=351 and y<364):
ser.write(78)
ser.write(',')
ser.write(58)
ser.write('/')
if (x>=364 and x <377) and (y>=364 and y<377):
ser.write(77)
ser.write(',')
ser.write(57)
ser.write('/')
if (x>=377 and x <389) and( y>=377 and y<389):
ser.write(76)
ser.write(',')
ser.write(56)
ser.write('/')
if (x>=389 and x <402) and( y>=389 and y<402):
ser.write(75)
ser.write(',')
ser.write(55)
ser.write('/')
if (x>=402 and x <415) and( y>=402 and y<415):
ser.write(74)
ser.write(',')
ser.write(54)
ser.write('/')
if (x>=415 and x <428) and( y>=415 and y<428):
ser.write(73)
ser.write(',')
ser.write(53)
ser.write('/')
if (x>=428 and x <441) and( y>=428 and y<441):
ser.write(72)
ser.write(',')
ser.write(52)
ser.write('/')
if (x>=441 and x <454) and( y>=441 and y<454):
ser.write(71)
ser.write(',')
ser.write(51)
ser.write('/')
if (x>=454 and x<467) and (y>=454 and y<467 ):
ser.write(70)
ser.write(',')
ser.write(50)
ser.write('/')
if (x>=467 and x<480) and (y>=467 and y<480):
ser.write(69)
ser.write(',')
ser.write(49)
ser.write('/')
if (x>=480 and x<493) and (y>=480 and y<493) :
ser.write(68)
ser.write(',')
ser.write(48)
ser.write('/')
if (x>=493 and x<506) and (y>=493 and y<506) :
ser.write(67)
ser.write(',')
ser.write(47)
ser.write('/')
if (x>=506 and x<519) and (y>=506 and y<519) :
ser.write(66)
ser.write(',')
ser.write(46)
ser.write('/')
if (x>=519 and x<522) and (y>=519 and y<522) :
ser.write(65)
ser.write(',')
ser.write(45)
ser.write('/')
if (x>=522 and x<530) and (y>=522 and y<530) :
ser.write(64)
ser.write(',')
ser.write(44)
ser.write('/')
# Display frame to user
cv.ShowImage("Target", color_image)
# Listen for ESC or ENTER key
c = cv.WaitKey(7) % 0x100
if c == 27 or c == 10:
break
if _name_=="__main__": t = Target() t.run() def manual(): # The function for manual mode
start.destroy() # #os.startfile (r"G:\project\disp.py") top=Tkinter.Tk() top.title("manual") top.overrideredirect(True) # Removes the borders from GUI x = (top.winfo_screenwidth() - top.winfo_reqwidth()) / 2 # Centering the GUI y = (top.winfo_screenheight() - top.winfo_reqheight()) / 2 top.geometry("+%d+%d" % (x-350, y-150)) path="G:\python\Untitled-4.jpg" # The path of the background image image = Image.open(path) tkp = ImageTk.PhotoImage(image) label_image = Tkinter.Label(top, image=tkp).grid(row=0,rowspan=2,column=0,columnspan=3) tkMessageBox.showinfo("MANUAL MODE","MANUAL MODE ACTIVATED") print 'manual' def OnKeyboardEvent(event): # This function gets executed whenever a key is pressed global count # Making the count variable global global count1 if event.Key=='Left': # Checking if the key pressed is Left print 'right' if(count<180): count=count+10 # Increasing the angle by 10 degree whenever the left key is pressed ser.write(count) # Sending the data in the format horz,vert/ ser.write(',') ser.write(count1) #ser.write('.') #ser.write('0') ser.write('/') if event.Key=='Right': # Checking if the key pressed is Right print 'left' if(count>0): count=count-10 # Decreasin the angle by 10 degree whenver the right key is pressed ser.write(count) # Sending the data in the format horz,vert/ ser.write(',') ser.write(count1) #ser.write('.') #ser.write('0') ser.write('/') if event.Key=='Up': count1=count1+10 if(count1<180): ser.write(count) ser.write(',') ser.write(count1) #ser.write('.') #ser.write('0') ser.write('/') if event.Key=='Down': if(count1>30): count1=count1-10 ser.write(count) ser.write(',') ser.write(count1) #ser.write('.') #ser.write('0') ser.write('/') if event.Key=='Space': ser.write(count) ser.write(',') ser.write(count1) #ser.write('.') #ser.write(10) ser.write('/') if event.Key=='Escape': tkMessageBox.showinfo("MANUAL MODE","EXITING MANUAL MODE") top.destroy() sys.exit() return True
hm = pyHook.HookManager() # Create a Hook Manager hm.KeyDown = OnKeyboardEvent # Watch for all Keyboard events hm.HookKeyboard() # Begins watching for keyboard events pythoncom.PumpMessages() # Wait forever
top.mainloop() def close1(): start.destroy()
start.overrideredirect(True) # Removes the borders from GUI x = (start.winfo_screenwidth() - start.winfo_reqwidth()) / 2 # Centering the GUI y = (start.winfo_screenheight() - start.winfo_reqheight()) / 2 start.geometry("+%d+%d" % (x-350, y-150)) path="G:\python\Untitled-2.jpg" # The path of the background image image = Image.open(path) tkp = ImageTk.PhotoImage(image)
""" Layout used is Grid layout
""" label_image = Tkinter.Label(start, image=tkp).grid(row=0,rowspan=2,column=0,columnspan=3) # Adding the image to GUI automatic=Tkinter.Button(start,text="AUTOMATIC",width=20,height=3,command=automatic).grid(row=1,column=0) # Creating a button which launches the automatic mode manual=Tkinter.Button(start,text="MANUAL",width=20,height=3,command=manual).grid(row=1,column=1) #Creating a button which launches the manual mode close=Tkinter.Button(start,text="EXIT",width=20,height=3,command=close1).grid(row=1,column=2) # Creating an exit button
start.mainloop() # Start the main loop of the GUI
code fore servo motor control:
#include Servo.h int serialdata1,serialdata2,serialdata3; int inbyte; int current=0; int pos=0; int count=0; Servo myservo; Servo myservo2;
void setup() { Serial.begin(9600); myservo.attach(3); myservo2.attach(9);
pinMode(11,OUTPUT);
}
void loop() {
while(Serial.available()>0) { count=count+1;
serialdata1=0;
serialdata2=0;
while(inbyte!=',')
{
inbyte=Serial.read();
if(inbyte > 0 && inbyte !=',')
{
serialdata1=serialdata1*10+inbyte-'0';
//Serial.println(serialdata1);
}
}
while(inbyte!='.')
{
inbyte=Serial.read();
if(inbyte > 0 && inbyte !='.')
{
serialdata2=serialdata2*10+inbyte-'0';
//Serial.println(serialdata2);
}
}
while(inbyte!='/')
{
inbyte=Serial.read();
if(inbyte > 0 && inbyte !='/')
{
serialdata3=serialdata3*10+inbyte-'0';
//Serial.println(serialdata2);
}
}
if(serialdata3==10)
{
digitalWrite(11,HIGH);
delay(200);
digitalWrite(11,LOW);
serialdata3=0;
}
}
myservo.write(serialdata1); myservo2.write(serialdata2);
} I will be expecting for the answer.
1 Comment
Walter Roberson
on 28 Jan 2014
What difference is there between this question and your existing http://www.mathworks.co.uk/matlabcentral/answers/113820-matlab-code-for-automated-defence-system ?
Answers (2)
Walter Roberson
on 28 Jan 2014
I am not going to translate a gun control system. If you want a gun control system then you can translate it, and you can be the person it is live-tested on.
I am a volunteer. I get to choose what I will or will not work on. And I will not work on weapons systems, not even for "defense". Not even as an "exercise".
0 Comments
David Sanchez
on 28 Jan 2014
Sorry shriroopa b, but, did you realize you are asking us to do a task more than asking a question? Your code seems endless. It would take some time to translate it into matlab. I see you are using OpenCV functions, which could make the job even more "entertaining".
0 Comments
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!