sobel edge detection java












0















Yet again im in trouble. I'm using an sobel edgedetector to get the edge of an image. Im using bresenhams algorithm to draw on the other end. But for that to happen i need solid coordinates.



My sobel returns 2 0's for each edge, and i just cant figure out why. If the image was twice as large it would make sense, but it is not.



In PAINT i made a 50x50 pixel image of a singe pixel square, that returns a 48x48 int array looking like this. the 48x48 i understand, but the 2 0's i do not understand.



255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255



the sobel i use comes from a method called magnitude array



public int getMagnitudeArray() {

int filter1 = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};

int filter2 = {
{1, 2, 1},
{0, 0, 0},
{-1, -2, -1}
};

Picture picture0 = new Picture(imagePath);
int width = picture0.width()-2;
int height = picture0.height()-2;
int arrayRepresentation = new int[width][height];

for (int y = 0; y < height ; y++) {
for (int x = 0; x < width ; x++) {

// get 3-by-3 array of colors in neighborhood
int gray = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray[i][j] = (int) Luminance.intensity(picture0.get(x + i, y + j));
}
}

// apply filter
int gray1 = 0, gray2 = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray1 += gray[i][j] * filter1[i][j];
gray2 += gray[i][j] * filter2[i][j];
}
}
// int magnitude = 255 - truncate(Math.abs(gray1) + Math.abs(gray2));
int magnitude = 255 - binaryTruncate((int) Math.sqrt(gray1 * gray1 + gray2 * gray2));
arrayRepresentation[x][y] = magnitude;
}
}

return arrayRepresentation;
} `


Is there anyone who can tell me why this method returns 2 0's and how i change it to return only 1, 0?



I tried to "fix" it after it returns the array with a hell og a lot of if statements, but it does not seem very elegant og efficiant.



Thanks.










share|improve this question

























  • By a single pixel square i mean a square drawn with a linethickness of one pixel. I believe the square is 42x42 pixels

    – Peter Lindhøj Tuxen
    Nov 24 '18 at 19:49
















0















Yet again im in trouble. I'm using an sobel edgedetector to get the edge of an image. Im using bresenhams algorithm to draw on the other end. But for that to happen i need solid coordinates.



My sobel returns 2 0's for each edge, and i just cant figure out why. If the image was twice as large it would make sense, but it is not.



In PAINT i made a 50x50 pixel image of a singe pixel square, that returns a 48x48 int array looking like this. the 48x48 i understand, but the 2 0's i do not understand.



255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255



the sobel i use comes from a method called magnitude array



public int getMagnitudeArray() {

int filter1 = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};

int filter2 = {
{1, 2, 1},
{0, 0, 0},
{-1, -2, -1}
};

Picture picture0 = new Picture(imagePath);
int width = picture0.width()-2;
int height = picture0.height()-2;
int arrayRepresentation = new int[width][height];

for (int y = 0; y < height ; y++) {
for (int x = 0; x < width ; x++) {

// get 3-by-3 array of colors in neighborhood
int gray = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray[i][j] = (int) Luminance.intensity(picture0.get(x + i, y + j));
}
}

// apply filter
int gray1 = 0, gray2 = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray1 += gray[i][j] * filter1[i][j];
gray2 += gray[i][j] * filter2[i][j];
}
}
// int magnitude = 255 - truncate(Math.abs(gray1) + Math.abs(gray2));
int magnitude = 255 - binaryTruncate((int) Math.sqrt(gray1 * gray1 + gray2 * gray2));
arrayRepresentation[x][y] = magnitude;
}
}

return arrayRepresentation;
} `


Is there anyone who can tell me why this method returns 2 0's and how i change it to return only 1, 0?



I tried to "fix" it after it returns the array with a hell og a lot of if statements, but it does not seem very elegant og efficiant.



Thanks.










share|improve this question

























  • By a single pixel square i mean a square drawn with a linethickness of one pixel. I believe the square is 42x42 pixels

    – Peter Lindhøj Tuxen
    Nov 24 '18 at 19:49














0












0








0








Yet again im in trouble. I'm using an sobel edgedetector to get the edge of an image. Im using bresenhams algorithm to draw on the other end. But for that to happen i need solid coordinates.



My sobel returns 2 0's for each edge, and i just cant figure out why. If the image was twice as large it would make sense, but it is not.



In PAINT i made a 50x50 pixel image of a singe pixel square, that returns a 48x48 int array looking like this. the 48x48 i understand, but the 2 0's i do not understand.



255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255



the sobel i use comes from a method called magnitude array



public int getMagnitudeArray() {

int filter1 = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};

int filter2 = {
{1, 2, 1},
{0, 0, 0},
{-1, -2, -1}
};

Picture picture0 = new Picture(imagePath);
int width = picture0.width()-2;
int height = picture0.height()-2;
int arrayRepresentation = new int[width][height];

for (int y = 0; y < height ; y++) {
for (int x = 0; x < width ; x++) {

// get 3-by-3 array of colors in neighborhood
int gray = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray[i][j] = (int) Luminance.intensity(picture0.get(x + i, y + j));
}
}

// apply filter
int gray1 = 0, gray2 = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray1 += gray[i][j] * filter1[i][j];
gray2 += gray[i][j] * filter2[i][j];
}
}
// int magnitude = 255 - truncate(Math.abs(gray1) + Math.abs(gray2));
int magnitude = 255 - binaryTruncate((int) Math.sqrt(gray1 * gray1 + gray2 * gray2));
arrayRepresentation[x][y] = magnitude;
}
}

return arrayRepresentation;
} `


Is there anyone who can tell me why this method returns 2 0's and how i change it to return only 1, 0?



I tried to "fix" it after it returns the array with a hell og a lot of if statements, but it does not seem very elegant og efficiant.



Thanks.










share|improve this question
















Yet again im in trouble. I'm using an sobel edgedetector to get the edge of an image. Im using bresenhams algorithm to draw on the other end. But for that to happen i need solid coordinates.



My sobel returns 2 0's for each edge, and i just cant figure out why. If the image was twice as large it would make sense, but it is not.



In PAINT i made a 50x50 pixel image of a singe pixel square, that returns a 48x48 int array looking like this. the 48x48 i understand, but the 2 0's i do not understand.



255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 0 0 0 0 0 0
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255
255 255 255 0 0 255 255 255 255 255 255



the sobel i use comes from a method called magnitude array



public int getMagnitudeArray() {

int filter1 = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};

int filter2 = {
{1, 2, 1},
{0, 0, 0},
{-1, -2, -1}
};

Picture picture0 = new Picture(imagePath);
int width = picture0.width()-2;
int height = picture0.height()-2;
int arrayRepresentation = new int[width][height];

for (int y = 0; y < height ; y++) {
for (int x = 0; x < width ; x++) {

// get 3-by-3 array of colors in neighborhood
int gray = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray[i][j] = (int) Luminance.intensity(picture0.get(x + i, y + j));
}
}

// apply filter
int gray1 = 0, gray2 = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gray1 += gray[i][j] * filter1[i][j];
gray2 += gray[i][j] * filter2[i][j];
}
}
// int magnitude = 255 - truncate(Math.abs(gray1) + Math.abs(gray2));
int magnitude = 255 - binaryTruncate((int) Math.sqrt(gray1 * gray1 + gray2 * gray2));
arrayRepresentation[x][y] = magnitude;
}
}

return arrayRepresentation;
} `


Is there anyone who can tell me why this method returns 2 0's and how i change it to return only 1, 0?



I tried to "fix" it after it returns the array with a hell og a lot of if statements, but it does not seem very elegant og efficiant.



Thanks.







java edge-detection sobel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 7:23









Bsquare ℬℬ

3,65071534




3,65071534










asked Nov 24 '18 at 19:47









Peter Lindhøj TuxenPeter Lindhøj Tuxen

102




102













  • By a single pixel square i mean a square drawn with a linethickness of one pixel. I believe the square is 42x42 pixels

    – Peter Lindhøj Tuxen
    Nov 24 '18 at 19:49



















  • By a single pixel square i mean a square drawn with a linethickness of one pixel. I believe the square is 42x42 pixels

    – Peter Lindhøj Tuxen
    Nov 24 '18 at 19:49

















By a single pixel square i mean a square drawn with a linethickness of one pixel. I believe the square is 42x42 pixels

– Peter Lindhøj Tuxen
Nov 24 '18 at 19:49





By a single pixel square i mean a square drawn with a linethickness of one pixel. I believe the square is 42x42 pixels

– Peter Lindhøj Tuxen
Nov 24 '18 at 19:49












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461789%2fsobel-edge-detection-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461789%2fsobel-edge-detection-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)