Computer Vision News - September 2016
Computer Vision News Trick 27 We will add a new function to this file: dataFromMatlab. This function will get the image as vector sent from Matlab, convert it “back” into a 3D image and perform the Hough Transform on it. Here is the Matlab and Python code for this: Trick Running the six lines of the Matlab code above would popup the Sudoku image with grid lines marked in blue, as we saw in the Tool section of last month . The new cv2 (OpenCV 3.X) interface for Python integrates numpy arrays into the OpenCV framework, which makes operations much simpler, as they are represented with simple multidimensional arrays. If you are using the OpenCV 2.X, you will have to use the cv.fromarray() function for converting the Numpy array into CV::Mat. For the trick of this month we have used: Win10, Matlab-2015b, Python-2.7, OpenCV 3.1 and Numpy-1.10. N.B. To call MATLAB functions from Python applications, see MATLAB Engine API for Python . The Matlab Code: Start by reading the image (lines 1-2), reshape it into a vector (line 3). Load our Python model ThePythonCode.py (line 4-5) and calling the dataFromMatlab Python function. Note that the py.reload Has a different syntax for Python 2.X 1. im = imread('dave.png'); 2. im = uint8(im); 3. asVec = reshape(im ,1 , []); 4. mod = py.importlib.import_module('ThePythonCode'); 5. py.reload(mod); 6. py.ThePythonCode.dataFromMatlab(l1 , size(im,1) , size(im,2) ) ; The Python Code: Lines 1-2: Reshape the vector back to an RGB image matrix of the size (nRow x nCol x 3) Lines 3-6: compute and display the HoughLines of the image. def dataFromMatlab( imageAsVector, nRow, nCol): 1. im = np.array(imageAsVector , dtype =np.uint8 ) 2. im = im.reshape(( 3 , nCol, nRow)).transpose().copy() 3. eg = getEdges( img ) 4. lines = getHoughlines(eg ) 5. img = PlotHoughLines( img, lines ) 6. plt.imshow(img) 7. plt.show()
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=