在位图中操纵位于中心圆外部的区域,使用C语言 – 建议?

huangapple go评论94阅读模式
英文:

Manipulating the area outside a centered circle in a bitmap, in C - suggestions?

问题

I have a 8bit-bitmap 1440:1080 image, with a centered circle on it. Now, I want to manipulate everything that's outside that circle. The image is pointed by the pointer *img.

I was thinking of doing it with distances, like this pseudo-code below:

  1. unsigned char* img;
  2. for (l = 0; l < width; l++) {
  3. for (j = 0; j < height; j++) {
  4. if(sqrt(pow(x2-x1,2) + pow(y2-y1,2)) > radius ){
  5. *(img + j * width + l) = 0;
  6. }
  7. }
  8. }

Like you probably noticed, my problem is how to relate x1,x2,y1,y2 to j and l.

I was thinking of making a function like this:

  1. typedef struct {
  2. int x;
  3. int y;
  4. } coord;
  5. coord getCoords(int index, int width){
  6. coord output;
  7. output.y=0;
  8. while(index>=width){
  9. index=index-width;
  10. output.y+=1;
  11. }
  12. output.x=index;
  13. return output;
  14. }

And the input index would be j * width + l for the pixel I'm trying to know if it's inside the circle or not. The center pixel is [720,540] or if I make it a float to be more precise [720.5, 540.5].

Am I wrong? Or is there a better way to do this?

PS: it's just simpler to create to local variables for coordinates inside the function, and do the math (the distance calculation) inside the function and return a bool. Instead of creating a new type of variable.

英文:

I have a 8bit-bitmap 1440:1080 image, with a centered circle on it. Now, I want to manipulate everything that's outside that circle. The image is pointed by the pointer *img.

I was thinking of doing it with distances, like this pseudo-code below:

  1. unsigned char* img;
  2. for (l = 0; l &lt; width; l++) {
  3. for (j = 0; j &lt; height; j++) {
  4. if(sqrt(pow(x2-x1,2) + pow(y2-y1,2)) &gt; radius ){
  5. *(img + j * width + l) = 0;
  6. }
  7. }
  8. }

Like you probably noticed, my problem is how to relate x1,x2,y1,y2 to j and l.

I was thinking of making a function like this:

  1. typedef struct {
  2. int x;
  3. int y;
  4. } coord;
  5. coord getCoords(int index, int width){
  6. coord output;
  7. output.y=0;
  8. while(index&gt;=width){
  9. index=index-width;
  10. output.y+=1;
  11. }
  12. output.x=index;
  13. return output;
  14. }

And the input index would be j * width + l for the pixel I'm trying to know if it's inside the circle or not. The center pixel is [720,540] or if I make it a float to be more precise [720.5, 540.5].

Am I wrong? Or is there a better way to do this?

PS: it's just simpler to create to local variables for coordinates inside the function, and do the math (the distance calculation) inside the function and return a bool. Instead of creating a new type of variable.

答案1

得分: 1

使用以下代码部分:

  1. pow(l-xc, 2) + pow(j-yc, 2) > pow(radius, 2)
英文:

Use

  1. pow(l-xc, 2) + pow(j-yc, 2) &gt; pow(radius, 2)

huangapple
  • 本文由 发表于 2023年5月29日 15:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定