# Sobel operator and 8 bit paletted image

This topic is 3125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I'm having few problem applying a Sobel filter to an image that I have loaded into the NDS (I'm programming it at libnds). I have to do this in indexed color (paletted 8 bit image). Here is the code I use:
void imageToSobel(u8* bmp, u16* palette, int len, int weight, int height) {
int i, j;
int x, y;
u8 p1, p2, p3, p4, p6, p7, p8, p9;
u8 R, G, B, gray;

for(i = 0; i < len/2; i++) {
R = palette & 0x1F;
G = (palette >> 5) & 0x1F;
B = (palette >> 10) & 0x1F;
gray = div32(30*R, 100) + div32(59*G, 100) + div32(11*B, 100);
BG_PALETTE = RGB15(gray, gray, gray);
}

for(i = 0; i < height; i++) {
for(j = 0; j < weight; j++) {
p1 = bmp[(i-1)*weight+(j-1)];
p2 = bmp[(i-1)*weight+j];
p3 = bmp[(i-1)*weight+(j+1)];
p4 = bmp[i*weight+(j-1)];
p6 = bmp[i*weight+(j+1)];
p7 = bmp[(i+1)*weight+(j-1)];
p8 = bmp[(i+1)*weight+j];
p9 = bmp[(i+1)*weight+(j+1)];

x = (p1+(p2+p2)+p3-p7-(p8+p8)-p9);
y = (p3+(p6+p6)+p9-p1-(p4+p4)-p7);
BG_GFX[i*weight+j] = sqrt32((x*x) + (y*y));
}
}
}


The first for loop converts the image to gray scale, and do it fine. But in the next for is the problem; it's seem that doesn't do correctly... I try the same code but loading a 16 bit image and works fine, so the algorithm is correct... Could somebody help me? I don't know where is the mistake :( Thank you! PD: Sorry about my bad English.

##### Share on other sites
maybe it's just a precision issue.
try:
gray = div32(30*R+59*G+11*B, 100);

##### Share on other sites
That doesn't fix the problem :/
The problem isn't it in the for which change the image into gray scale; is in the second for, which apply the sobel filter :(

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 26
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633384
• Total Posts
3011595
• ### Who's Online (See full list)

There are no registered users currently online

×