I try to obtain the adjacency matrix of weights, and then use it in the calculation of the minimum weight path. There is a problem, when I try to display it, I get a wrong result :
By logic, the diagonal must have only 0, and in the places where the vertices are adjacent, must be the weight of the edge
//set the source and destination of each edge
g->edge[0]->src = 0;
g->edge[0]->dest = 1;
g->edge[0]->weight = 9;
g->edge[1]->src = 0;
g->edge[1]->dest = 10;
g->edge[1]->weight = 6;
g->edge[2]->src = 1;
g->edge[2]->dest = 2;
g->edge[2]->weight = 3;
g->edge[3]->src = 1;
g->edge[3]->dest = 10;
g->edge[3]->weight = 2;
g->edge[4]->src = 2;
g->edge[4]->dest = 3;
g->edge[4]->weight = 2;
g->edge[5]->src = 2;
g->edge[5]->dest = 6;
g->edge[5]->weight = 3;
g->edge[6]->src = 2;
g->edge[6]->dest = 5;
g->edge[6]->weight = 3;
g->edge[7]->src = 3;
g->edge[7]->dest = 4;
g->edge[7]->weight = 5;
g->edge[8]->src = 4;
g->edge[8]->dest = 5;
g->edge[8]->weight = 4;
g->edge[9]->src = 6;
g->edge[9]->dest = 10;
g->edge[9]->weight = 2;
g->edge[10]->src = 6;
g->edge[10]->dest = 7;
g->edge[10]->weight = 9;
g->edge[11]->src = 7;
g->edge[11]->dest = 8;
g->edge[11]->weight = 7;
g->edge[12]->src = 7;
g->edge[12]->dest = 9;
g->edge[12]->weight = 2;
g->edge[13]->src = 8;
g->edge[13]->dest = 9;
g->edge[13]->weight = 7;
g->edge[14]->src = 9;
g->edge[14]->dest = 10;
g->edge[14]->weight = 5;
My code :
for (i = 0; i < numberOfVertices; i++)
{
adjacency_matrix[i][i] = 0;
for (j = i + 1; j < numberOfVertices; j++)
{
adjacency_matrix[i][j] = g->edge[i]->weight;
adjacency_matrix[j][i] = g->edge[i]->weight;
}
}
What's wrong?
for (i = 0; i < numberOfVertices; i++)
{
adjacency_matrix[i][i] = 0;
for (j = i + 1; j < numberOfVertices; j++)
{
adjacency_matrix[i][j] = g->edge[i]->weight;
adjacency_matrix[j][i] = g->edge[i]->weight;
}
}
In this code you are setting every edge from vertex i to every other vertex to the same weight. I do not think this is what you want.
( Note: It is hard to know what you do want. When reporting a problem you need to include a description of both what happens AND what you wanted to happen. "It's wrong!" is almost useless as a bug report. )
Related
I have implemented bilinear interpolation algorithm according to the MATLAB example:
https://se.mathworks.com/matlabcentral/fileexchange/10772-fast-2-dimensional-interpolation; https://en.wikipedia.org/wiki/Bilinear_interpolation - Unit square formula.
Would be great to speed up the implementation. Can somebody give me suggestions how to optimize the code for speeding it up? Please, find part of the method:
x_loops = floor((X_end-X_11)/pixel_size_mm)+1;
y_loops = floor((Y_end-Y_11)/pixel_size_mm)+1;
float** Zi = new float*[x_loops] ();
for(int i = 0; i < x_loops; ++i)
Zi[i] = new float[y_loops] ();
n_dx = 1/(X_12 - X_11);
n_dy = 1/(Y_21 - Y_11);
Yi = Y_11;
int count = 0;
for(int i = 0; i < y_loops; i++)
{
Xi = X_11;
xi = 0;
yi = 0;
for(int j = 0; j < x_loops; j++)
{
xi = (Xi - X_11)*n_dx;
yi = (Yi - Y_11)*n_dy;
Xi += pixel_size_mm;
fxi = floor(xi);
fyi = floor(yi);
dfxi = xi - fxi;
dfyi = yi - fyi;
Zi[j][i] = (Strain_image[fxi][fyi]*(1 - dfxi)*(1-dfyi) +
Strain_image[fxi+1][fyi]*dfxi*(1-dfyi) + Strain_image[fxi][fyi+1]*
(1-dfxi)*dfyi + Strain_image[fxi+1][fyi+1]*dfxi*dfyi);
}
Yi += pixel_size_mm;
}
iMage(Zi, x_loops, y_loops,fX1,fY1);
for(int i = 0; i < x_loops; ++i)
delete [] Zi[i];
delete[] Zi;
for(int i = 0; i < number_of_RF_rows; ++i)
delete [] Strain_image[i];
delete[] Strain_image;
I am using Win 7,64, MS2012(C++) and OpenCV 2.4.11
I write seeded region growing algorithm but I down't know why the results not desired.
The goal is that extract pectoral muscle(left corner) of image with SRG algorithm.
input image :
here is my code (main function)
img = imread("C:/Users/Zimabi/Downloads/region_growing/jhgray.jpg",0);
Mat J1 = Mat::zeros(img.rows,img.cols,CV_8UC1);
int x=15,y=15;
int reg_mean = img.at<uchar>(x,y);
int reg_size = 1;
int neg_pos = 0;
Mat neg_list = Mat::zeros(img.cols*img.rows,3,CV_8UC1);
double pixdist = 0, reg_maxdist = 0.1;
int xn = 0,yn = 0;
Mat neigb = Mat::zeros(4,2,CV_8UC1);
int nei1 [4] = {-1,1,0,0};
int nei2 [4] = {0,0,-1,1};
while ((pixdist < reg_maxdist) && (reg_size < img.cols*img.rows))
{
int inx,min1=1000;
for (int r = 0; r < 4; r++)
{
xn = x + nei1[r] ;
yn = y + nei2[r];
bool ins=(xn>=1)&&(yn>=1)&&(xn<=img.rows)&&(yn<= img.cols);
if (ins && (J1.at<uchar>(xn,yn) == 0))
{
neg_pos = neg_pos+1;
neg_list.at<uchar>(neg_pos,0) = xn;
neg_list.at<uchar>(neg_pos,1) = yn;
neg_list.at<uchar>(neg_pos,2) = img.at<uchar>(xn,yn);
J1.at<uchar>(xn,yn)=255;
}
}
Mat x1 = Mat::zeros(neg_pos,1,CV_8UC1);
for (int i3 = 0; i3 <neg_pos ; i3++)
{
x1.at<uchar>(i3,0) = abs(neg_list.at<uchar>(i3,2) - reg_mean);
if (x1.at<uchar>(i3,0)<min1)
{
min1 = x1.at<uchar>(i3,0);
inx = i3;
}
}
pixdist = min1;
J1.at<uchar>(x,y)=255;
reg_size=reg_size+1;
reg_mean= (reg_mean*reg_size + neg_list.at<uchar> (inx,2))/(reg_size+1);
x = neg_list.at<uchar>(inx,0);
y = neg_list.at<uchar>(inx,1);
neg_list.at<uchar>(inx,0) = neg_list.at<uchar>(neg_pos,0);
neg_list.at<uchar>(inx,1) = neg_list.at<uchar>(neg_pos,1);
neg_list.at<uchar>(inx,2) = neg_list.at<uchar>(neg_pos,2);
neg_pos=neg_pos-1;
}
imshow("J",J1);
waitKey(0);
return 0;
Regards
here is the matlab code i'm trying to convert to c++
where
size(Iorig) == 1334X 2026
%% label checkers
Label = zeros(size(Iorig));
Margins = 11;
[X,Y] = meshgrid(1:size(Iorig,2),1:size(Iorig,1));
k = 1;
for i = 1:4
for j = 1:6
rr = rect{i,j};
x1 = rr(1);
x2 = rr(1) + rr(3);
y1 = rr(2);
y2 = rr(2) + rr(4);
Label(X>=x1+Margins&X<x2-Margins&Y>=y1+Margins&Y<y2-Margins) = k;
k = k+1;
end
end
I understand that we want to label the rectangles which are found in the previous step, there are 24 of those.
but I don't understand how to convert this line into easy c++ code without allocating a huge buffer of X and Y which basically just holds... indices..
thanks for your help here is what i started doing.
//label Checkers
List<List<int>^>^ label = gcnew List<List<int>^>();
int margins = 11;
int k = 1;
for (size_t i = 0; i < 4; i++)
{
for (size_t j = 0; j < 6; j++)
{
MacbethCheckerBatchesColor^ rect = autoDetectMacbethResult[i * 6 + j];
Point^ r = rect->Points[0];
int x1 = r->X;
int y1 = r->Y;
r = rect->Points[2];
int x2 = r->X;
int y2 = r->Y;
for (int h = 0; h < inputImage->HeightLines; h++)
{
List<int>^ tempRow = gcnew List<int>();
for (int w = 0; w < inputImage->WidthColumns; w++)
{
if ( (w>= x1+margins) & (w<x2-margins) & (h >= y1+margins) & (h<y2-margins) )
{
tempRow->Add(k);
}
else
{
tempRow->Add(0);
}
}
label->Add(tempRow);
}
k= k+100;//i tried here many other numbers... same result
}
}
Here is my result can you please help me find my mistake, the rectangles are the same, I guesss I have some other logical mistake.
I got some memory allocated in a loop - how to free it when I am done with tr_data variable ?
(I am fairly new to C++)
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
struct svm_problem tr_data;
tr_data.l = (int) prm_num_samples_anchored.array[bar];
tr_data.y = Malloc(double, tr_data.l);
tr_data.x = Malloc(struct svm_node*, tr_data.l);
for (int row = 0; row < tr_data.l; row++)
{
tr_data.y[row] = ta0.array[bar-row-1];
//leak
svm_node* tr_data_x_onerow = Malloc(svm_node, num_features+1);
tr_data_x_onerow[0].index = 1; tr_data_x_onerow[0].value = in0.array[bar-row-1]; tr_data_x_onerow[1].index = 2; tr_data_x_onerow[1].value = in1.array[bar-row-1]; tr_data_x_onerow[2].index = 3; tr_data_x_onerow[2].value = in2.array[bar-row-1]; tr_data_x_onerow[3].index = 4; tr_data_x_onerow[3].value = in3.array[bar-row-1]; tr_data_x_onerow[4].index = 5; tr_data_x_onerow[4].value = in4.array[bar-row-1]; tr_data_x_onerow[5].index = 6; tr_data_x_onerow[5].value = in5.array[bar-row-1]; tr_data_x_onerow[6].index = 7; tr_data_x_onerow[6].value = in6.array[bar-row-1]; tr_data_x_onerow[7].index = 8; tr_data_x_onerow[7].value = in7.array[bar-row-1]; tr_data_x_onerow[8].index = 9; tr_data_x_onerow[8].value = in8.array[bar-row-1]; tr_data_x_onerow[9].index = 10;
tr_data_x_onerow[num_features].index = -1; //Each row of properties should be terminated with a -1 according to the readme
tr_data.x[row] = tr_data_x_onerow;
}
... few operation on tr_data
... and this does not work
for (int row = 0; row <tr_data.l; row++)
{
free(tr_data_x_onerow);
}
for (int row = 0; row <tr_data.l; row++)
{
free(tr_data.x[row]);
}
But please, just don't do this. This is C++. Use a vector or some other sane collection.
I have such a code:
QVector<Point> legalMoves = field.getLegalMoves();
QVector<Cell> costs;
costs.reserve(legalMoves.size());
for (QVector<Point>::iterator i = legalMoves.begin(); i < legalMoves.end(); ++i)
costs.push_back(checkCost(field, *i, player, -100, 100));
Cell cost;
if (player) {
QVector<Point>::iterator m1 = legalMoves.begin();
QVector<Cell>::iterator m2 = costs.begin();
QVector<Cell>::iterator j = costs.begin() + 1;
for (QVector<Point>::iterator i = legalMoves.begin() + 1; i < legalMoves.end(); ++i, ++j)
if (j->status > m2->status) {
m1 = i;
m2 = j;
}
cost=*m2;
}
else {
QVector<Point>::iterator m1 = legalMoves.begin();
QVector<Cell>::iterator m2 = costs.begin();
QVector<Cell>::iterator j = costs.begin() + 1;
for (QVector<Point>::iterator i = legalMoves.begin() + 1; i < legalMoves.end(); ++i, ++j)
if (j->status < m2->status) {
m1 = i;
m2 = j;
}
cost=*m2;
}
QVector<Point> moves;
QVector<Cell>::iterator j = costs.begin();
for (QVector<Point>::iterator i = legalMoves.begin(); i < legalMoves.end(); ++i, ++j)
if (j->status == cost.status)
moves.push_back(*i);
short index = qrand()%moves.size();
return moves[index];
}
When i'm debugging it, compiler just skips this parts inside loops:
if (j->status < m2->status) {
m1 = i;
m2 = j;
}
which means the functions returns the very first Point(or any other Point with same Cell value instead of min/max). Why does that happen and how is it possible to fix it?