1 Dimensional Sobel Produces Noise - c++

I'm attempting a piece-by-piece Sobel edge detector for a project for school, and I can't wrap my head around where I am going wrong. Without putting up too much detail, I think a large portion of it boils down to the code below. When I put a lenna.pgm image:
through for a 2D mask along the x-gradient, I get a lot of noise.
I discussed the code with my instructor, and I'm doing what he says to do.
Here's the code for the x-direction convolution:
void applySobel(int maskX[3][3], int maskY[3][3], int maskWidth, int imageH, int imageW,
int threshold, int*** generated){
int sumX, sumY;
// convolve smoothed image with Sobel mask in the X-direction
for(int i = 0; i < imageH; i++) {
for(int j = 0; j < imageW; j++) {
if(i == 0 || i >= imageH - 1 || j == 0 || j >= imageW - 1) {
sumX = 0;
} else {
sumX = 0;
for(int x = -1; x <= 1; x++) {
for(int y = -1; y <= 1; y++) {
sumX += generated[0][i+x][j+y] * maskX[x+1][y+1];
}
}
}
generated[1][i][j] = sumX;
}
}
}
I've also tried normalizing the image before outputting to a file, but the image goes dark.
for(int a = 1; a < 6; a++) {
min = imageOUT[a][0][0];
max = 0;
// normalize the pixel values and then write to files
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
pixel = imageOUT[a][i][j];
if(pixel < min) {
min = pixel;
} if(pixel > max) {
max = pixel;
}
}
}
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
imageOUT[a][i][j] = (int)(imageOUT[a][i][j] - min) * (255/(max-min));
}
}
WriteImage(fileOutName[a-1].c_str(), imageOUT[a], M, N, Q);
}
I deeply appreciate any insight. This has been keeping me up for days now.

UPDATE: Here's the solution I arrived at. Basically, I took only the objects of interest in the mask instead of multiplying and adding all of it, so the zero spaces were left out.
void applySobel(int maskX[3][3], int maskY[3][3], int maskWidth, int imageH, int imageW,
int threshold, int*** generated){
int sumX, sumY;
// convolve smoothed image with Sobel mask in the X-direction
for(int i = 0; i < imageH; i++) {
for(int j = 0; j < imageW; j++) {
if(i == 0 || i == imageH - 1 || j == 0 || j == imageW - 1) {
sumX = generated[0][i][j];
} else {
sumX = (int)(generated[0][i-1][j-1]*maskX[0][0] +
generated[0][i][j-1]*maskX[1][0] +
generated[0][i+1][j-1]*maskX[2][0] +
generated[0][i-1][j+1]*maskX[0][2] +
generated[0][i][j+1]*maskX[1][2]+
generated[0][i+1][j+1]*maskX[2][2])/2;
}
generated[1][i][j] = sumX/3;
}
}
And the beautiful Lenna after applying the X-Direction Gradient:
Thank you all very much for your suggestions.

Related

How do I change the values in a 2d array using 1d array?

I want to make the values of num[] replace all of the a[][] values when one of the numbers equal k. So all the values of num correspond to each place at k. So for example when k reaches 20 if there is a 20 in the 2d array I want to replace all 20's with whatever is in num[19], but whenever I try it that, they all become skewed numbers and I can't find the reason why. Are my for loops set up wrong or what else could be the problem?
#include<iostream>
int main(){
//other code that uses a file to make 2d array
int width, height, maxval;
fin >> P2 >> width >> height >> maxval;
int **a = new int *[height];
for (int i = 0; i < height; i++)
a[i] = new int[width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
fin >> a[i][j];
}
}
}
void eq(int **a, int h, int w) {
int num[255];
double num1[255];
double prob[255], cumul[255]{ 0 };
double x, y, z=0;
for (int k = 1; k <= 255; k++)
{
int temp = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {//counts the repeated pixel values
if (pix[i][j] == k)
{
temp += 1;
}
}
}
num1[k - 1] = temp;
}
for (int i = 0; i < 255; i++) {
prob[i] = (num1[i] / (height*width));//show the decimal number of how many pixel values are in the image over the total
cout << prob[i] << endl;
}
for (int i = 0; i < 255; i++) {
y = prob[i];
x = y + z;
z = x;// adds all the probabilities to make the sum
cumul[i] =x;
cout << cumul[i] << endl;
}
for (int i = 0; i < 255; i++) {
num[i] =floor(cumul[i]*255);// converts the cumulative number to the new pixel value and sets it in a array
}
for (int k = 1; k <= 255; k++) {//loop that is not coming out right
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == k)
{
a[i][j] =num[k-1];
}
}
}
}
}
Basically I am making a function that deals with histogram equalization to make a pgm image clearer. In the main function I am calling a pgm file and setting all the pixel values into a 2d array. So the num[] is the part where I make the new pixel values, but for some reason whenever I call for example a[0][0] I should be getting something that is not zero or 255 since both of those values mean that none of the pixels had that corresponding intensity, but whenever I call it I get 255 or some other random number.
If i understand what you want to do, this line :
a[i][j] =num[i];
should be :
a[i][j] =num[k-1];
Because you want:
if( a[i][j] = k = 20){
a[i][j] = num[k-1 = 19]
}

Comparing pixels in alpha-trimmed filter

I have the following problem. I have written code for alpha-trimmed filter in opencv library. I think that it is properly constructed but I don't know how to compare two 3 channels pixels during sorting a 'window with pixels'. In my code it is done but comparing two but it is impossible for vectors. I assume that i should compare it one channel and after second and so on. Have you any hints for me, or could you propose some modifications in my code. This is my code.
int alphatrimmed(Mat img, int alpha)
{
Mat img9 = img.clone();
const int start = alpha;
const int end = 9 - alpha;
//going through whole image
for (int i = 1; i < img.rows - 1; i++)
for (int j = 1; j < img.cols-1; j++)
{
int k = 0;
Vec3b element[9];
//selecting elements
for (int m = i - 1; m < i + 2; m++)
for (int n = j - 1; n < j + 2; n++)
element[k++] = img.at<Vec3b>(m*img.cols + n);
for (int i = 0; i < end; i++)
{
int min = i;
for (int j = i + 1; j < 9; j++)
if (element[j] < element[min])
min = j;
Vec3b temp = element[i];
element[i] = element[min];
element[min] = temp;
}
const int result = (i - 1)*(img.cols - 2) + j - 1;
img9.at<Vec3b>(result) = element[start];
for (int j = start + 1; j < end; j++)
img9.at<Vec3b>(result) += element[j];
img9.at<Vec3b>(result) /= 9 - alpha;
}
namedWindow("AlphaTrimmed Filter", WINDOW_AUTOSIZE);
imshow("AlphaTrimmed Filter", img9);
return 0;
}
Thank you for your time spent on solving my problem.

window operation by Pointer of image

If we access pixel by a pointer using step and data of Mat Image. see example below
int step = srcimg.step;
for (int j = 0; j < srcimg.rows; j++) {
for (int i = 0; i < srcimg.cols; i++) {
//this is pointer to the pixel value.
uchar* ptr = srcimg.data + step* j + i;
}
}
Question:
How can we perform 3x3 weighted avg operations with image step by a pointer?
thanks
You mustn't use data field in opencv because memory is not allways continuous. you can check this using isContinuous() method.
Now you can do like this (image type is CV_8UC1)
for (int i = 1; i < srcimg.rows-1; i++)
{
for (int j = 1; j < srcimg.cols-1; j++)
{
int x=0;
for (int k=-1;k<=1;k++)
{
uchar* ptr=srcimg.ptr(k+i)+j-1;
for (int l=-1;l<=1;l++,ptr++)
x +=*ptr;
}
}
}
image border are not processed. Now if you want to blur an image use blur method
You can use this post too
I am doing something like this .
int sr = 3;
for (int j = 0; j < srcimg.rows; j++) {
for (int i = 0; i < srcimg.cols; i++) {
uchar* cp_imptr = im.data;
uchar* tptr = im.data + imstep *(sr + j) + (sr + i);
int val_tptr = cp_imptr [imstep *(sr + j) + (sr + i)]; //pointer of image data amd step at 3x3
int val_cp_imptr = cp_imptr[imstep *j + i];
double s = 0;
for (int n = templeteWindowSize; n--;)
{
for (int m = templeteWindowSize; m--;)
{
uchar* t = tptr; //pointer of template
// sum
s += *t;
t++;
}
t += cstep;
}
}
cout << endl;
}

How to find unique labels for segments in SuperpixelSLIC

I am using cv::ximgproc::SuperpixelSLIC opencv c++ to generate segments of image. I want each segment label to be unique. Here is my code.
Mat segmentImage() {
int num_iterations = 4;
int prior = 2;
bool double_step = false;
int num_levels = 10;
int num_histogram_bins = 5;
int width, height;
width = h1.size().width;
height = h1.size().height;
seeds = createSuperpixelSLIC(h1);
Mat mask;
seeds->iterate(num_iterations);
Mat labels;
seeds->getLabels(labels);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) == 0)
cout << i << " " << j << " " << labels.at<int>(i, j) << endl;
}
}
ofstream myfile;
myfile.open("label.txt");
myfile << labels;
myfile.close();
seeds->getLabelContourMask(mask, false);
h1.setTo(Scalar(0, 0, 255), mask);
imshow("result", h1);
imwrite("result.png", h1);
return labels;
}
In label.txt file I observe that label 0 has been given to two segments (i.e. segment include pixel(0,0) and pixel(692,442). These two segments are pretty far away.
Is this normal thing or my code is incorrect. Please help me to find unique label for each segment.
What you essentially need is a connected components algorithm. Without knowing the exact SLIC implementation you use, SLIC usually tends to produce disconnected superpixels, i.e. disconnected segments with the same label. A simple solution I used is the connected components algorithm form here: https://github.com/davidstutz/matlab-multi-label-connected-components (originally from here: http://xenia.media.mit.edu/~rahimi/connected/). Note that this repository contains a MatLab wrapper. In your case you only need connected_components.h together with the following code:
#include "connected_components.h"
// ...
void relabelSuperpixels(cv::Mat &labels) {
int max_label = 0;
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) > max_label) {
max_label = labels.at<int>(i, j);
}
}
}
int current_label = 0;
std::vector<int> label_correspondence(max_label + 1, -1);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
int label = labels.at<int>(i, j);
if (label_correspondence[label] < 0) {
label_correspondence[label] = current_label++;
}
labels.at<int>(i, j) = label_correspondence[label];
}
}
}
int relabelConnectedSuperpixels(cv::Mat &labels) {
relabelSuperpixels(labels);
int max = 0;
for (int i = 0; i < labels.rows; ++i) {
for (int j = 0; j < labels.cols; ++j) {
if (labels.at<int>(i, j) > max) {
max = labels.at<int>(i, j);
}
}
}
ConnectedComponents cc(2*max);
cv::Mat components(labels.rows, labels.cols, CV_32SC1, cv::Scalar(0));
int component_count = cc.connected<int, int, std::equal_to<int>, bool>((int*) labels.data, (int*) components.data, labels.cols,
labels.rows, std::equal_to<int>(), false);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
labels.at<int>(i, j) = components.at<int>(i, j);
}
}
// component_count would be the NEXT label index, max is the current highest!
return component_count - max - 1;
}
On the obtained labels, run relabelConnectedSuperpixels.

better algorithm for counting points in grids

Suppose I have a point P in [0,1]*[0,1], and [0,1] is divided into m(say 200) grids. I use A[m][m] to indicate whether [a small square centred at P with length 2h] covers each grid or not. So for a point P, A[i][j] is either (increase by) 1 or 0.
Suppose I have n such points(P1,...,Pn), I want to calculate A(for each point Pi, I redo the above procedure, adding 1 or not). How can I do this efficiently(with C++) rather than writing 3 layers of for loops to check for each grid and each point(So O(nm^2))?
I tried the naive 3 for loops with C++. It takes longer time than using some of the vectorized operations(like vector<= number for comparing n numbers together, A[bool vector, bool vector] for subsetting) in R.
Since C++ is generally faster than R, is there any smart way to implement this process?
#include <Rcpp.h>
#include <cmath>
using namespace Rcpp;
// [[Rcpp::export]]
double myfun(NumericVector u, NumericVector v)
{
double n = u.size();
double A[200][200] = {0};
double pos[200];
int i = 0, j = 0, k = 0;
for (i = 0; i < 200; i++)
{
pos[i] = (double)i / 201;
}
for (k = 0; k < n; k++)
{
for (i = 0; i < 200; i++)
{
for (j = 0; j < 200; j++)
{
if ( (fabs(u[k] - pos[i]) <= h) && (fabs(v[k] - pos[j]) <=h ) )
{
A[i][j]++;
}
}
}
}
double s = 0, avg = 0;
for (i = 0; i <200; i++)
{
for (j = 0; j < 200; j++)
{
s += A[i][j];
}
}
avg = s / (200 * 200);
return (avg);
}
The two inner loops only determine index of the point in your grid. But you can compute the index directly:
int i = (int)(u[k]*200);
int j = (int)(v[k]*200);
You probably also need to check that i and j don't reach the index 200. This only happens though, when u[k] == 1.0 or v[k] == 1.0.
double n = u.size();
double A[200][200] = {0};
for (int k = 0; k < n; k++)
{
int i = (int)(u[k]*200);
int j = (int)(v[k]*200);
if (i == 200)
i = 199;
if (j == 200)
j = 199;
A[i][j]++;
}