Last active
August 29, 2015 14:09
-
-
Save vatsan/6fa971ab1785d1a6f6f0 to your computer and use it in GitHub Desktop.
native_apps_blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Gautam Muralidhar and Srivatsan Ramanujam, 28 Oct 2014 | |
* C++ functions compiled into dynamic library to be invoked from PL/Python via ctypes. | |
* Canny Edge Detection from OpenCV. | |
*/ | |
#include "opencv2/opencv.hpp" | |
#include "opencv2/imgproc/imgproc.hpp" | |
#include <stdio.h> | |
#include <string.h> | |
#include <iostream> | |
#include <vector> | |
using namespace cv; | |
using namespace std; | |
extern "C" { | |
// Find edges in images using Open CV’s implementation of Canny’s edge detection algorithm | |
// Inputs: a) char * bytes - the raw image byte stream, and b) uint nBytes - the number of bytes in the byte stream | |
// Output: an unsigned integer array of 1s amd 0s, where 1s denote edge locations. | |
// The length of the returned array = the number of image rows x number of image columns. | |
uint* edgeDetectionFromByteStream(char *bytes, uint nBytes){ | |
Mat srcImg, srcGray; | |
Mat dstImg, onesImg, edges; | |
vector<unsigned char> src; | |
for (int i = 0; i < nBytes; i++) { | |
src.push_back(bytes[i]); | |
} | |
// Read the image from the buffer in memory using the OpenCV imdecode function | |
srcImg = imdecode(src, CV_LOAD_IMAGE_COLOR); | |
if(srcImg.data ) { | |
dstImg.create( srcImg.size(), CV_8UC1 ); | |
onesImg.create( srcImg.size(), CV_8UC1 ); | |
// Convert the input image to gray-scale using the OpenCV cvtColor function | |
cvtColor( srcImg, srcGray, COLOR_BGR2GRAY ); | |
// Smooth the gray-scale image to reduce noise by using the OpenCV blur function. | |
blur(srcGray, edges, Size(3,3)); | |
// Call the OpenCV Canny function to find edges. | |
Canny(edges, edges, 10, 30, 3 ); | |
// Create an image of 1’s and 0’s, where 1 denotes an edge pixel. | |
dstImg = Scalar::all(0); | |
onesImg = Scalar::all(1); | |
onesImg.copyTo(dstImg,edges); | |
// Prepare the final result array | |
uint* result = new uint[dstImg.rows*dstImg.cols]; | |
for (int i = 0; i < dstImg.rows; i++){ | |
for (int j = 0; j < dstImg.cols; j++){ | |
result[(edges.cols)*i+j] = uint(dstImg.at<unsigned char>(i,j)); | |
} | |
} | |
return result; | |
} else { | |
// Return a single element 0 array if there is a problem loading the data | |
uint* result = new uint[1]; | |
result[0] = 0; | |
return result; | |
} | |
} | |
} | |
extern "C" { | |
// Get the image size from the raw image byte stream. | |
// Inputs: a) char * bytes - the raw image byte stream, and b) uint nBytes - the number of bytes in the byte stream | |
// Output: an unsigned integer array comprising of two elements - number of rows and number of columns | |
uint* getImgSizeFromByteStream(char *bytes, uint nBytes){ | |
// Declare the OpenCV source image matrix | |
Mat srcImg; | |
// Copy the input byte stream into a C++ vector of unsigned char | |
vector<unsigned char> src; | |
for (int i = 0; i < nBytes; i++) { | |
src.push_back(bytes[i]); | |
} | |
// Read the image from the buffer in memory using OpenCV’s imdecode function | |
srcImg = imdecode(src, CV_LOAD_IMAGE_COLOR); | |
uint* result = new uint[2]; | |
result[0] = srcImg.rows; | |
result[1] = srcImg.cols; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment