Skip to content

Instantly share code, notes, and snippets.

@vatsan
Last active August 29, 2015 14:09
Show Gist options
  • Save vatsan/11a7e745e9af37d3a652 to your computer and use it in GitHub Desktop.
Save vatsan/11a7e745e9af37d3a652 to your computer and use it in GitHub Desktop.
/**
* Gautam Muralidhar, Nov 2014
* Internal C++ functions for Canny Edge Detection.
*
**/
#include <iostream>
#include <string>
#include <vector>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdint.h>
using namespace std;
void Tokenize(
const string& str,
vector<string>& tokens,
const string& delimiters = " ") {
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
bool edgeDetectionFromByteStream(vector<int8_t> src, uint* result){
cv::Mat srcImg, srcGray, dstImg, onesImg, edges;
srcImg = cv::imdecode(src, CV_LOAD_IMAGE_COLOR);
if(srcImg.data ) {
///Create a matrix of the same type and size as src (for dst)
dstImg.create(srcImg.size(), CV_8UC1 );
onesImg.create(srcImg.size(), CV_8UC1 );
/// Convert the image to grayscale
cv::cvtColor(srcImg, srcGray, cv::COLOR_BGR2GRAY);
/// Blur the image first
cv::blur(srcGray, edges, cv::Size(3,3));
/// Call Canny's edge detect
cv::Canny(edges, edges, 10, 30, 3 );
dstImg = cv::Scalar::all(0);
onesImg = cv::Scalar::all(1);
onesImg.copyTo(dstImg,edges);
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 true;
} else {
return false;
}
}
uint* getImgSizeFromByteStream(vector<int8_t> src){
cv::Mat srcImg;
srcImg = cv::imdecode(src, CV_LOAD_IMAGE_COLOR);
if (srcImg.data) {
uint* result = new uint[2];
result[0] = srcImg.rows;
result[1] = srcImg.cols;
return result;
} else {
uint* result = new uint[2];
result[0] = -1;
result[1] = -1;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment