Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created March 1, 2019 20:18
Show Gist options
  • Save fernandoc1/c862f16d2d4bc46fb6b8f1809a87fdc8 to your computer and use it in GitHub Desktop.
Save fernandoc1/c862f16d2d4bc46fb6b8f1809a87fdc8 to your computer and use it in GitHub Desktop.
This code plays sound of shapes
all:
g++ morph_descriptor.cpp -o MorphDescriptor -lao -lopencv_core -lopencv_highgui -lopencv_imgproc
#include <iostream>
#include <cmath>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <ao/ao.h>
#include <math.h>
#define BUF_SIZE 4096
void playBuffer(std::vector<float>& data)
{
ao_device *device;
ao_sample_format format;
int default_driver;
char *buffer;
int buf_size;
int sample;
//float freq = 440.0;
float freq = 220.0;
int i;
ao_initialize();
default_driver = ao_default_driver_id();
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 2;
//format.rate = 44100;
format.rate = 22050;
format.byte_format = AO_FMT_LITTLE;
/* -- Open driver -- */
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
fprintf(stderr, "Error opening device.\n");
return;
}
/* -- Play some stuff -- */
buf_size = format.bits/8 * format.channels * format.rate * 5;
buffer = (char*)calloc(buf_size,
sizeof(char));
//for (i = 0; i < format.rate; i++)
for (i = 0; i < buf_size; i++)
{
buffer[i] = data[i % data.size()];
}
ao_play(device, buffer, buf_size);
/* -- Close and shutdown -- */
ao_close(device);
ao_shutdown();
}
float directionDistance(cv::Mat1b& img, cv::Point center, float angle)
{
angle = (M_PI * angle) / 180;
int x0 = center.x;
int y0 = center.y;
int x1 = center.x + 1000 * cos(angle);
int y1 = center.y + 1000 * sin(angle);
int dx = abs(x1-x0);
int dy = abs(y1-y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx-dy;
cv::Point pt;
while(true)
{
pt = cv::Point(x0, y0);
if((pt.x < img.cols) && (pt.y < img.rows) && (pt.x > 0) && (pt.y > 0))
{
uchar value = img.at<uchar>(pt);
if(value == 0) { break; }
}
else { break; }
if ((x0==x1) && (y0==y1)) { break; }
int e2 = 2*err;
if (e2 >-dy){ err -= dy; x0 += sx; }
if (e2 < dx){ err += dx; y0 += sy; }
}
return cv::norm(center - pt);
}
void captureRadialFeatures(cv::Mat1b& img, float step, std::vector<float>& featureVec)
{
cv::Moments m = cv::moments(img, true);
cv::Point center(m.m10/m.m00, m.m01/m.m00);
for(float i = 0; i < 360; i += step)
{
featureVec.push_back(directionDistance(img, center, i));
}
}
int main(int argc, char** argv)
{
if(argc < 2)
{
std::cout << "Usage: " << argv[0] << " <image_file>" << std::endl;
return 0;
}
cv::Mat1b image = cv::imread(argv[1], 0);
cv::Mat1b threshImg;
cv::threshold(image, threshImg, 128, 255, cv::THRESH_BINARY_INV);
cv::Mat1b auxImg;
threshImg.copyTo(auxImg);
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(auxImg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
std::vector<cv::Rect> boundRect;
cv::RNG rng(12345);
cv::Mat drawing = cv::Mat::zeros(image.size(), CV_8UC3);
for(int i = 0; i < contours.size(); i++)
{
cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255));
cv::drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0);
boundRect.push_back(cv::boundingRect(contours[i]));
cv::rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
}
cv::Mat drawingRoiImg(drawing, boundRect[0]);
cv::Mat1b roiImg(threshImg, boundRect[0]);
std::vector<float> featureVec;
captureRadialFeatures(roiImg, 0.01, featureVec);
playBuffer(featureVec);
//for(int i = 0; i < featureVec.size(); i++)
//{
// std::cout << featureVec[i] << std::endl;
//}
cv::imshow("DISPLAY", image);
cv::imshow("DISPLAY_THRESH", threshImg);
cv::imshow("DRAWING", drawing);
cv::imshow("ROI", roiImg);
cv::waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment