Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "utility.h"
#include "math.h"
std::string utility::intToString(int number)
{
std::stringstream ss; //create a stringstream
ss << number; //add number to the stream
return ss.str(); //return a string with the contents of the stream
}
/* ---------Compute edges and find circles using hough transform----------------*/
void utility::findCircles(image& src, ROI roi, int t, int r)
{
image G, tgt, G_Thresholded; // for amplitudes
G.resize(src.getNumberOfRows(), src.getNumberOfColumns());
G_Thresholded.resize(src.getNumberOfRows(), src.getNumberOfColumns());
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (i != 0 && j != 0 && i != src.getNumberOfRows() - 1 && j != src.getNumberOfColumns() - 1) {
double Gx = -1 * src.getPixel(i - 1, j - 1) + src.getPixel(i - 1, j + 1)
- 2 * src.getPixel(i, j - 1) + 2 * src.getPixel(i, j + 1)
- 1 * src.getPixel(i + 1, j - 1) + src.getPixel(i + 1, j + 1);
Gx = abs((int)Gx);
double Gy = -1 * src.getPixel(i - 1, j - 1) - 2 * src.getPixel(i - 1, j) - src.getPixel(i - 1, j + 1)
+ src.getPixel(i + 1, j - 1) + 2 * src.getPixel(i + 1, j) + src.getPixel(i + 1, j + 1);
Gy = abs((int)Gy);
double GM = sqrt(pow(Gx, 2) + pow(Gy, 2));
G.setPixel(i, j, (int)GM);
}
}
else {
G.setPixel(i, j, src.getPixel(i, j));
}
}
G.save("Gradient.pgm");
utility::binarizeGrey(G, tgt, roi, t, t);
tgt.save("Gradient_Thresholded.pgm");
image PSpace;
PSpace.resize(tgt.getNumberOfRows(), tgt.getNumberOfColumns());
for (int i = 0; i < tgt.getNumberOfRows(); i++)
for (int j = 0; j < tgt.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1 && tgt.getPixel(i, j) == 255) {
for (int angle = 0; angle < 360; angle++) {
double a = i - r * cos(angle);
double b = j - r * sin(angle);
PSpace.setPixel((int)a, (int)b, PSpace.getPixel((int)a, (int)b) + 1);
}
}
else {
tgt.setPixel(i, j, src.getPixel(i, j));
}
}
PSpace.save("PSpace.pgm");
for (int ROI = 0; ROI < roi.getSize(); ROI++) {
int max = 0;
int xx, yy;
for (int i = 0; i < PSpace.getNumberOfRows(); i++)
for (int j = 0; j < PSpace.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1 && value == ROI) {
if (PSpace.getPixel(i, j) > max) {
max = PSpace.getPixel(i, j);
xx = i;
yy = j;
}
}
}
for (int m = r - 1; m <= r + 1; m++)
for (int angle = 0; angle < 360; angle++) {
double a = xx - m * cos(angle);
double b = yy - m * sin(angle);
if (roi.InROI((int)a, (int)b) != -1)
src.setPixel((int)a, (int)b, 255);
}
}
}
/* ---------Compute edges using sober operator on grayscale images----------------*/
void utility::computeEdges(image& src, ROI roi, int thres, int dir)
{
image G, tgt, direction, G_Thresholded, G_direction_T; // for amplitude and direction
G.resize(src.getNumberOfRows(), src.getNumberOfColumns());
direction.resize(src.getNumberOfRows(), src.getNumberOfColumns());
G_Thresholded.resize(src.getNumberOfRows(), src.getNumberOfColumns());
G_direction_T.resize(src.getNumberOfRows(), src.getNumberOfColumns());
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (i != 0 && j != 0 && i != src.getNumberOfRows() - 1 && j != src.getNumberOfColumns() - 1) {
double Gx = -1 * src.getPixel(i - 1, j - 1) + src.getPixel(i - 1, j + 1)
- 2 * src.getPixel(i, j - 1) + 2 * src.getPixel(i, j + 1)
- 1 * src.getPixel(i + 1, j - 1) + src.getPixel(i + 1, j + 1);
Gx = abs((int)Gx);
double Gy = -1 * src.getPixel(i - 1, j - 1) - 2 * src.getPixel(i - 1, j) - src.getPixel(i - 1, j + 1)
+ src.getPixel(i + 1, j - 1) + 2 * src.getPixel(i + 1, j) + src.getPixel(i + 1, j + 1);
Gy = abs((int)Gy);
double GM = sqrt(pow(Gx, 2) + pow(Gy, 2));
double theta = (atan(Gy / Gx) * 180.0) / 3.141592;
G.setPixel(i, j, (int)GM);
direction.setPixel(i, j, (int)theta);
}
}
else {
G.setPixel(i, j, src.getPixel(i, j));
direction.setPixel(i, j, 0);
}
}
G.save("Gradient.pgm");
utility::binarizeGrey(G, tgt, roi, thres, thres);
tgt.save("Gradient_Thresholded.pgm");
utility::directionGrey(tgt, direction, roi, dir, dir);
tgt.save("Gradient_Direction_Thresholded.pgm");
}
void utility::findCirclesC(image& src, ROI roi, int t, int r)
{
image tmp;
tmp.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
double I = (src.getPixel(i, j, RED) + src.getPixel(i, j, GREEN) + src.getPixel(i, j, BLUE)) / 3;
tmp.setPixel(i, j, (int)I);
}
image G, tgt, G_Thresholded; // for amplitudes
G.resize(src.getNumberOfRows(), src.getNumberOfColumns());
G_Thresholded.resize(src.getNumberOfRows(), src.getNumberOfColumns());
tgt.resize(tmp.getNumberOfRows(), tmp.getNumberOfColumns());
for (int i = 0; i < tmp.getNumberOfRows(); i++)
for (int j = 0; j < tmp.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (i != 0 && j != 0 && i != tmp.getNumberOfRows() - 1 && j != tmp.getNumberOfColumns() - 1) {
double Gx = -1 * tmp.getPixel(i - 1, j - 1) + tmp.getPixel(i - 1, j + 1)
- 2 * tmp.getPixel(i, j - 1) + 2 * tmp.getPixel(i, j + 1)
- 1 * tmp.getPixel(i + 1, j - 1) + tmp.getPixel(i + 1, j + 1);
Gx = abs((int)Gx);
double Gy = -1 * tmp.getPixel(i - 1, j - 1) - 2 * tmp.getPixel(i - 1, j) - tmp.getPixel(i - 1, j + 1)
+ tmp.getPixel(i + 1, j - 1) + 2 * tmp.getPixel(i + 1, j) + tmp.getPixel(i + 1, j + 1);
Gy = abs((int)Gy);
double GM = sqrt(pow(Gx, 2) + pow(Gy, 2));
G.setPixel(i, j, (int)GM);
}
}
else {
G.setPixel(i, j, tmp.getPixel(i, j));
}
}
fflush(stdout);
G.save("Gradient.pgm");
utility::binarizeGrey(G, tgt, roi, t, t);
tgt.save("Gradient_Thresholded.pgm");
image PSpace;
PSpace.resize(tgt.getNumberOfRows(), tgt.getNumberOfColumns());
for (int i = 0; i < tgt.getNumberOfRows(); i++)
for (int j = 0; j < tgt.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1 && tgt.getPixel(i, j) == 255) {
for (int angle = 0; angle < 360; angle++) {
double a = i - r * cos(angle);
double b = j - r * sin(angle);
PSpace.setPixel((int)a, (int)b, PSpace.getPixel((int)a, (int)b) + 1);
}
}
else {
tgt.setPixel(i, j, tmp.getPixel(i, j));
}
}
PSpace.save("PSpace.pgm");
for (int ROI = 0; ROI < roi.getSize(); ROI++) {
int max = 0;
int xx, yy;
for (int i = 0; i < PSpace.getNumberOfRows(); i++)
for (int j = 0; j < PSpace.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1 && value == ROI) {
if (PSpace.getPixel(i, j) > max) {
max = PSpace.getPixel(i, j);
xx = i;
yy = j;
}
}
}
for (int m = r - 1; m <= r + 1; m++)
for (int angle = 0; angle < 360; angle++) {
double a = xx - m * cos(angle);
double b = yy - m * sin(angle);
if (roi.InROI((int)a, (int)b) != -1) {
src.setPixel((int)a, (int)b, RED, 255);
src.setPixel((int)a, (int)b, GREEN, 0);
src.setPixel((int)a, (int)b, BLUE, 0);
}
}
}
}
void utility::computeEdgesC(image& src, ROI roi, int thres, int dir)
{
image tmp;
tmp.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
double I = (src.getPixel(i, j, RED) + src.getPixel(i, j, GREEN) + src.getPixel(i, j, BLUE)) / 3;
tmp.setPixel(i, j, (int)I);
}
image G, tgt, direction, G_Thresholded; // for amplitudes
G.resize(src.getNumberOfRows(), src.getNumberOfColumns());
G_Thresholded.resize(src.getNumberOfRows(), src.getNumberOfColumns());
direction.resize(src.getNumberOfRows(), src.getNumberOfColumns());
tgt.resize(tmp.getNumberOfRows(), tmp.getNumberOfColumns());
for (int i = 0; i < tmp.getNumberOfRows(); i++)
for (int j = 0; j < tmp.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (i != 0 && j != 0 && i != tmp.getNumberOfRows() - 1 && j != tmp.getNumberOfColumns() - 1) {
double Gx = -1 * tmp.getPixel(i - 1, j - 1) + tmp.getPixel(i - 1, j + 1)
- 2 * tmp.getPixel(i, j - 1) + 2 * tmp.getPixel(i, j + 1)
- 1 * tmp.getPixel(i + 1, j - 1) + tmp.getPixel(i + 1, j + 1);
Gx = abs((int)Gx);
double Gy = -1 * tmp.getPixel(i - 1, j - 1) - 2 * tmp.getPixel(i - 1, j) - tmp.getPixel(i - 1, j + 1)
+ tmp.getPixel(i + 1, j - 1) + 2 * tmp.getPixel(i + 1, j) + tmp.getPixel(i + 1, j + 1);
Gy = abs((int)Gy);
double GM = sqrt(pow(Gx, 2) + pow(Gy, 2));
double theta = (atan(Gy / Gx) * 180.0) / 3.141592;
// printf("%f\n",theta);
G.setPixel(i, j, (int)GM);
direction.setPixel(i, j, (int)theta);
}
}
else {
G.setPixel(i, j, tmp.getPixel(i, j));
direction.setPixel(i, j, 0);
}
}
G.save("Gradient.pgm");
utility::binarizeGrey(G, tgt, roi, thres, thres);
tgt.save("Gradient_Thresholded.pgm");
utility::directionGrey(tgt, direction, roi, dir, dir);
tgt.save("Gradient_Direction_Thresholded.pgm");
}
/*----------------------------Grey thresholding---------------------------------**/
void utility::binarizeGrey(image& src, image& tgt, ROI roi, int value1, int value2)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0)
value = value1;
else
value = value2;
if (src.getPixel(i, j) > value)
tgt.setPixel(i, j, 255);
else if (src.getPixel(i, j) < value)
tgt.setPixel(i, j, 0);
}
else {
tgt.setPixel(i, j, src.getPixel(i, j));
}
}
}
/*----------------------------Thresholding using direction info---------------------------------**/
void utility::directionGrey(image& src, image& dr, ROI roi, int value1, int value2)
{
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1 && (src.getPixel(i, j) == 255)) {
if (value == 0)
value = value1;
else
value = value2;
int t1 = value + 10;
if (t1 > 90)
t1 = 90;
int t2 = value - 10;
if (t2 < 0)
t2 = 0;
if (dr.getPixel(i, j) < t1 && dr.getPixel(i, j) > t2)
src.setPixel(i, j, 255);
else
src.setPixel(i, j, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment