Mon*_*RPG 2 c# algorithm image-processing
我正在使用C#在visual studio 2010上编写软件.这个软件做的是在选择正方形后在图像上找到相同的正方形.每个方块由16x16像素组成.我当前的算法从第一个像素开始并逐个像素地扫描整个图像,以确定与所选像素相同的像素方块.这需要很长时间.你能建议我更好的方法吗?
也订购了每个广场.所以他们从0 - 16 - 32 - 48开始
一个正方形不能从5或65等开始
谢谢
您可以缓存每个图像区域的校验和.然后你只需要检查与校验和匹配的那些是否相等.
假设每个图像都是16x16 rgb元素.你可以这样做(是的,它会有整数溢出.)
所有这些都是伪代码 - 您可以将其转换为C#.
将int添加到字段图像类,或创建一个图像包装器,其中int为"校验和"
int checksum = 0
for each pixel in image {
checksum += pixel.red + pixel.blue + pixel.green
// you could do anything you wanted here, like
// checksum *= 17 + pixel.red
// checksum *= 17 + pixel.blue
// checksum *= 17 + pixel.green
// just make it "unique enough", like a hashcode
}
image.checksum = checksum
Run Code Online (Sandbox Code Playgroud)
现在,当你去搜索时,你可以这样:
/**
* equals method before:
*/
boolean equals(image a, image b) {
for x = 0..15 do /* all 16 pixels in X */
for y = 0..15 do /* all 16 pixels in Y */
if a.getPixel(x,y) != b.getPixel(x,y) return false;
return true;
}
/**
* equals method after:
*.
boolean equals(image a, image b) {
/* this check lets you skip the loop in most cases */
/* still have to verify that the image is equal pixel for pixel though */
if a.checksum != b.checksum return false;
for x = 0..15 do /* all 16 pixels in X */
for y = 0..15 do /* all 16 pixels in Y */
if a.getPixel(x,y) != b.getPixel(x,y) return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1965 次 |
最近记录: |