0
執行這段代碼時,程序使用matlabcontrol庫打開MATLAB並在eval()中執行給定的行。問題出現在第一行,即m.eval("image1=imread('D:/My Hand Gestures/f.jpg');");
將固定的字符串值作爲輸入。但是,在這裏我想將路徑存儲在一個變量中,並將它傳遞給imread()
函數。我應該怎麼做?任何幫助表示讚賞。這是代碼。將變量值傳遞給eval函數
package Interface;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
/**
*
* @author Rajdeep
*/
public class Output {
private static String check;
private static String path;
Output(){
//default constructor
}
Output(String s,String p){
check = s;
path=p;
}
//GrayScale Conversion Function
public static void grayScale(String p,MatlabProxy m)throws MatlabConnectionException, MatlabInvocationException{
m.eval("image1=imread('D:/My Hand Gestures/f.jpg');");
m.feval("image1","imread(path)");
m.eval("figure,imshow(image1);");
m.eval("image_gray=rgb2gray(image1);");
m.eval("figure,imshow(image_gray);");
m.eval("final_image=imresize(image_gray,0.03125);");
m.eval("figure,imshow(final_image);");
m.eval("imwrite(final_image,'C:/Users/Desktop/f.jpg');");
//Disconnect the proxy from MATLAB
m.disconnect();
}
//Median Filtering Function
public static void Filter(String p, MatlabProxy m)throws MatlabConnectionException, MatlabInvocationException{
m.eval("I=imread('D:/gestures/f.jpg');");
m.eval("J = medfilt2(I, [4 4]);");
m.eval("figure,imshow(J);");
m.eval("imwrite(J,'C:/Users/Rajdeep/Desktop/f.jpg');");
//Disconnect the proxy from MATLAB
m.disconnect();
}
/**
*
* @param args
* @throws MatlabConnectionException
* @throws MatlabInvocationException
*/
public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
//Create a proxy, which we will use to control MATLAB
MatlabProxyFactory factory = new MatlabProxyFactory();
MatlabProxy proxy = factory.getProxy();
Output out=new Output("GrayScale","D:/My Hand Gestures/f.jpg");
if(check == "GrayScale") {
grayScale(path, proxy);
}
if(check== "Filter"){
Filter(path,proxy);
}
}
}
這裏我創建了一個具有預定義路徑的路徑變量。我想使用這個變量,而不是像上面提到的過程那樣給出路徑。
謝謝,這確實管用。你救了我很多麻煩。 – Rajdeep
就這麼簡單。我從來沒有這樣想過。 – Rajdeep