Reputation: 585
I've been working in a ColorSegmentationAlgorithm with openCV. I finished the segmentation, but now I'm collecting the object on the picture by color. In order to do that I've got the "rows" of the image compressed in RLE and they are refered each other with pointers.
At this point (I think) that everything is all right because I printed on the console the results of "clusters" compressed in RLE and everything seems to be ok. Then I look over the list of structures to create a new variable that will represent the object (With bouncin box, centroid, etc.). But, here's the trouble, I don't now exactly why when call the content of the pointer in a "random case" it's bad loaded (It's like variables were shifted): for example if the structure has this content:
(int i, int je, int js, "pointer" *child) --> {1, 1 , 1, 0x0AB0231A}
its loaded --> {1, 0x0AB0231A, 1, 1}
And If the next "child" is called then it provoke a segmentation fault (Core dumped) error
So... Here is a part of the code:
struct LineObjRLE {
unsigned int i;
unsigned int js;
unsigned int je;
unsigned int size;
unsigned int color;
struct LineObjRLE *parent;
struct LineObjRLE *child;
};
struct LineObjRLE auxRLE = aRLE[i][j];
vector<struct LineObjRLE> obj;
while (1) {
obj.push_back(auxRLE);
printf("auxRLE: parent: %d -- child: %d\n",
auxRLE.parent, auxRLE.child);
if (auxRLE.child == NULL)
break;
auxRLE = *auxRLE.child;
}
Here I've got some results: (The pointers are displayed as int, but they are right)
---------------------------------
k: 1
auxRLE: parent: 0 -- child: 55035088
auxRLE: parent: 55036080 -- child: 55505600
auxRLE: parent: 55035088 -- child: 0
---------------------------------
k: 2
auxRLE: parent: 0 -- child: 55035248
auxRLE: parent: 0 -- child: 0
---------------------------------
k: 3
auxRLE: parent: 0 -- child: 55035288
auxRLE: parent: 55036200 -- child: 55505720
auxRLE: parent: 55035288 -- child: 0
---------------------------------
k: 4
auxRLE: parent: 0 -- child: 55035528
auxRLE: parent: 0 -- child: 55505840
auxRLE: parent: 55035528 -- child: 0
---------------------------------
k: 5
auxRLE: parent: 0 -- child: 55035688
auxRLE: parent: 0 -- child: 0
---------------------------------
k: 6
auxRLE: parent: 0 -- child: 55035808
auxRLE: parent: 18 -- child: 6
Segmentation fault (core dumped)
The segmentation occurs when "the shift" that I described before happens. When I examined all the content of the structure I saw that the pointers where in other variables of the structure and the pointer has other values (so point to 6 or 18 generate the error).
PD: Instead of saving also "parents" pointer, I only use the "child" direction for look over the structures.
I hope I explained my self, and someone could help me. Thanks in advance!
CODE:
while (waitKey(1)) {
t1 = clock(); // Time counter1
device >> frame; // Get the frame from the camera
imshow("Ori", frame); // Show the original frame
//medianBlur(frame, frame, 5);
imageBGR2HSV(&frame); // Transform image from BGR to HSV
int n = frame.channels(); // Count the number of image's channels to use the pointer
int color;
short int js, colorRLE = -1, jRLE = -1; // Variables for RLE encode
for (int i = 0; i < frame.rows; i++) {
uchar* ptr = frame.ptr<uchar>(i); // Pointer to i row
vector<struct LineObjRLE> temp;
for (int j = 0; j < frame.cols; j++) {
// Proximate the color to a cluster
color = CS.whichColorHSV(
(color3cInt ) { ptr[n * j], ptr[n * j + 1], ptr[n
* j + 2] });
// RLE encoding
if (!j) {
colorRLE = color;
jRLE = 1;
js = 0;
} else {
if (j == frame.cols - 1) {
temp.push_back((LineObjRLE ) { i, js, j, j - js, color,
NULL, NULL });
} else if (color == colorRLE) {
jRLE = jRLE + 1;
} else if (color != colorRLE) {
temp.push_back((LineObjRLE ) { i, js, j, j - js, color,
NULL, NULL });
colorRLE = color;
jRLE = 1;
js = j;
}
}
// Change the color (Improve assigning directly the BGR color, save from using imageHSV2BGR)
if (color != -1) {
ptr[n * j] = CS.ColorCluster[color].a;
ptr[n * j + 1] = CS.ColorCluster[color].b;
ptr[n * j + 2] = CS.ColorCluster[color].c;
} else {
ptr[n * j] = CS.ColorCluster[0].a;
ptr[n * j + 1] = CS.ColorCluster[0].b;
ptr[n * j + 2] = CS.ColorCluster[0].c;
}
}
aRLE.push_back(temp);
if (i) { // Except the first line that can't be child of any object (only parent) start joining objects grouped in LineObjRLE variables
unsigned int j = 0, jp = 0; // Pointer to current and previous LineObjRLE
unsigned int pp = aRLE[i - 1][jp].size, pc = aRLE[i][j].size; // Pointer to previous and current col
bool end = false; // Flag to manage the loop
while (!end) {
if ((aRLE[i - 1][jp].je > aRLE[i][j].js
&& aRLE[i - 1][jp].js < aRLE[i][j].je)
&& aRLE[i - 1][jp].color == aRLE[i][j].color) {
aRLE[i][j].parent = &(aRLE[i - 1][jp]);
aRLE[i - 1][jp].child = &(aRLE[i][j]);
//printf("Parent is %d and says that child is: %d\n", &aRLE[i - 1][jp], aRLE[i - 1][jp].child);
//printf("Child is %d and says that parent is: %d\n", &aRLE[i][j], aRLE[i][j].parent);
}
if (j == aRLE[i].size() - 1 || jp == aRLE[i - 1].size() - 1)
end = true;
if (pp > pc) {
pc += aRLE[i][j].size;
j++;
} else {
pp += aRLE[i - 1][jp].size;
jp++;
}
}
}
}
// Run vertically to identifies the parents of the objects
int k = 0; //Index for final object
for (unsigned int i = 0; i < aRLE.size(); i++) {
for (unsigned int j = 0; j < aRLE.at(i).size(); j++) {
if (aRLE[i][j].parent == NULL && aRLE[i][j].child != NULL) {
printf("---------------------------------\n");
printf("k: %d \n", k);
printf("aRLE: parent: %d -- child: %d\n",
aRLE[i][j].parent, aRLE[i][j].child);
// Grow from the seed
struct LineObjRLE auxRLE = aRLE[i][j];
vector<struct LineObjRLE> obj;
while (1) {
obj.push_back(auxRLE);
printf("auxRLE: parent: %d -- child: %d\n",
auxRLE.parent, auxRLE.child);
if (auxRLE.child == NULL)
break;
auxRLE = *auxRLE.child;
}
k = k + 1;
}
}
}
// Calculate increment of time
t2 = clock();
float dif = (((float) t2 - (float) t1) / 1000000.0F) * 1000;
printf("FINISHED IN %f \n", dif);
imageHSV2BGR(&frame);
imshow("Viewer", frame); // Show the image segmented
aRLE.clear();
objs.clear();
}
return 0;
}
Upvotes: 0
Views: 275
Reputation: 154025
Looking at the code, it seems you are storing pointers to objects inside the std::vector<RLE>
. For example, I spotted:
aRLE[i][j].parent = &(aRLE[i - 1][jp]);
At the same time I don't see any call to aRLE.reserve()
which would ensure that the objects don't get relocated upon adding new objects. I suspect, that the vector needs to reallocate memory, releasing already pointed to memory, and you get a bunch of stale pointers.
The easiest ways to test the theory if you create stale pointers are those:
std::vector<T>::reserve(size_type n)
to tell the data structure to allocate sufficient space to hold up to n
elements.std::vector<RLE>
by std::deque<RLE>
: Although adding objects at either end may change iterators, it won't change the location of objects.Upvotes: 1