From aa80ec5a7e08850e3a3314853071c39daba37e2b Mon Sep 17 00:00:00 2001 From: Joan Arnaldich Date: Tue, 1 May 2012 20:23:13 +0200 Subject: [PATCH] Backup --- Makefile | 1 + buscacos.cpp | 135 +++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index 08397e9..68a39cc 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ CXXFLAGS += `/usr/local/bin/pkg-config opencv --cflags` # Linking (add flags as needed) LDFLAGS += `/usr/local/bin/pkg-config opencv --libs` +LDFLAGS += -lgdal # Name your target executables here all = buscacos diff --git a/buscacos.cpp b/buscacos.cpp index d885ff8..ef9ba97 100644 --- a/buscacos.cpp +++ b/buscacos.cpp @@ -1,6 +1,9 @@ +#ifdef _MSC_VER #include "stdafx.h" +#endif //#include +#include #include #include //#include "cxcore.h" @@ -120,10 +123,10 @@ int minimax_cost(Mat& img, Point2i startPoint, Point2i endPoint, Mat& imgFlooded -inline int pseudo_random() +inline unsigned int pseudo_random() { - static int v = rand(); - static int u = rand(); + static unsigned int v = rand(); + static unsigned int u = rand(); v = 36969*(v & 65535) + (v >> 16); u = 18000*(u & 65535) + (u >> 16); return (v << 16) + u; @@ -140,7 +143,7 @@ void get_watershed_markers(Mat& costImg, int iMaxCost, Mat& markers, Mat& imgFlo Mat_::iterator it, itEnd = threshold.end(); unsigned int count = 0; unsigned int max_count = INT_MAX; - while(max_count > 6000) + while(max_count > 32768) { max_count = 0; for(it=threshold.begin(), count = 0; it != itEnd; it++, count++) @@ -199,7 +202,7 @@ void get_watershed_relief(Mat& costImg, Mat& imgFlooded, Mat& reliefBGR) //XXXX: OJUUU!!! subtract(Scalar(255), costImg, relief); - cvtColor(relief, reliefBGR, CV_GRAY2BGR); + cvtColor(relief, reliefBGR, CV_GRAY2BGR); // } void get_raster_graph(Mat& costImg, Mat& raster_graph, Point2i startPoint, Point2i endPoint) @@ -379,9 +382,9 @@ void calculate_dijkstra(vector& p, v.x /= v.count; v.y /= v.count; unsigned int curr_dist_start = - static_cast(_hypot(v.x - startPoint.x, v.y - startPoint.y)); + static_cast(hypot(v.x - startPoint.x, v.y - startPoint.y)); unsigned int curr_dist_end = - unsigned int(_hypot(v.x - endPoint.x, v.y - endPoint.y)); + static_cast(hypot(v.x - endPoint.x, v.y - endPoint.y)); if (curr_dist_start < start_dist) { @@ -520,38 +523,76 @@ inline unsigned int absdiff(int a, int b) void find_nearest_vertex(Point2i point, Mat& landscape, Mat& raster_graph, Mat& breadcrumb) { - int x = point.x, y= point.y; - int max_diff = 0, curr_val, window_val; - while(raster_graph.at(y, x) == 0) - { - curr_val = landscape.at(y, x); - - for(int dy = -1; dy <= 1; dy++) - for(int dx = -1; dx <= 1; dx++) - { - window_val = landscape.at(y+dy, x+dx); - diff = absdiff(curr_val, window_val); - if(disconnected.at(y+dy, x+dx) == 255) - { - if - x += dx; - y += dy; - bFound = true; - } - } - - breadcrumb.at(y, x) = 255; + int x = point.x, y= point.y, max_diff = 0, curr_val, window_val, diff, max_x, max_y, min_val; + int randx, randy; + for(int path_len = 512; path_len > 0 && (raster_graph.at(y, x) == 0); path_len-- ) + { + curr_val = landscape.at(y, x); + max_diff = 0; + min_val = INT_MAX; + randx = rand();//pseudo_random(); + randy = rand(); //pseudo_random(); + for(int dy = 0; dy < 3; dy++) + { + for(int dx = 0; dx < 3; dx++) + { + if((dy == 1) && (dx == 1)) + continue; + + int iwiny = y + (dy + randy)%3 - 1; + int iwinx = x + (dx + randx)%3 - 1; + + if(breadcrumb.at(iwiny, iwinx) == 255) // Ja visitat + continue; + + if((breadcrumb.at(iwiny - 1, iwinx - 1) + + breadcrumb.at(iwiny - 1, iwinx) + + breadcrumb.at(iwiny - 1, iwinx + 1) + + breadcrumb.at(iwiny, iwinx - 1) + + breadcrumb.at(iwiny, iwinx) + + breadcrumb.at(iwiny, iwinx + 1) + + breadcrumb.at(iwiny + 1, iwinx - 1) + + breadcrumb.at(iwiny + 1, iwinx) + + breadcrumb.at(iwiny + 1, iwinx + 1)) >= 255*4) + continue; + + + + window_val = landscape.at(iwiny, iwinx); + // Anem cap avall. L'actual ha de ser mes alt... + // diff = absdiff(curr_val, window_val); //curr_val - window_val; + printf("(%d, %d) = %d, %d -> %d - %d\n", (dx + randx)%3 - 1, (dy + randy)%3 - 1, iwinx, iwiny, curr_val, window_val); + + if(window_val < min_val) + { + // diff = max_diff; + min_val = window_val; + max_x = iwinx; + max_y = iwiny; + } + } + } - } + breadcrumb.at(y, x) = 255; + x = max_x; + y = max_y; + printf("\n"); + } } int main(int argc, char **argv) { GDALAllRegister(); - char* fname_1 = "f:\\BuscaCos2\\83690004.tif"; - char* fname_2 = "f:\\BuscaCos2\\83690005.tif"; - Graph graph; - Mat raster_graph, vertexs, vertex_labels, costImg; + /* + char* fname_1 = "83690004.tif"; + char* fname_2 = "83690005.tif"; + 83610025 + */ + char* fname_1 = "83610025.tif"; + char* fname_2 = "83620024.tif"; + + Graph graph; + Mat raster_graph, vertexs, vertex_labels, costImg; /* Mat imgMaxLevel; @@ -596,7 +637,7 @@ int main(int argc, char **argv) Mat disconnected(raster_graph - (vertexs > 0)); if(disconnected.type() == CV_8U) - printf("Tipus guai\n"); + printf("Tipus guai\n"); Mat disconnected_backup; disconnected.copyTo(disconnected_backup); @@ -609,11 +650,7 @@ int main(int argc, char **argv) startPoint, endPoint, disconnected, graph); - imwrite("DISCONNECTED.tif", disconnected); - - - - + // imwrite("DISCONNECTED.tif", disconnected); // return(0); @@ -633,7 +670,7 @@ int main(int argc, char **argv) Edge& e = graph[edgeId]; e.drawLine(disconnected, 2); - if(_hypot(e.x1 - v.x, e.y1 - v.y) < _hypot(e.x2 - v.x, e.y2 - v.y)) + if(hypot(e.x1 - v.x, e.y1 - v.y) < hypot(e.x2 - v.x, e.y2 - v.y)) msk_follow_edge(disconnected_backup, seamline, e.x1, e.y1, graph); else msk_follow_edge(disconnected_backup, seamline, e.x2, e.y2, graph); @@ -645,21 +682,11 @@ int main(int argc, char **argv) } } - - find_nearest_vertex(startPoint, costImg, raster_graph, seamline); - /*** - boost::tie(vertexIt, vertexEnd) = vertices(graph); - for(; vertexIt != vertexEnd; ++vertexIt) - { - std::cout << "parent(" << *vertexIt; - if (p[*vertexIt] == *vertexIt) - std::cout << ") = no parent" << std::endl; - else - std::cout << ") = " << p[*vertexIt] << std::endl; - } - ***/ - // Mat* toMerge[] = { &seamline, &disconnected, &disconnected }; + printf("Nearest vertex...\n"); + imwrite("RASTER2.tif", raster_graph); + find_nearest_vertex(startPoint, costImg, raster_graph, seamline); + vector channels(3); channels[2] = (disconnected + seamline); -- 2.11.4.GIT