我已经创建了一个函数来查找图像中的颜色,并返回x,y.现在我需要添加一个新功能,在那里我可以找到具有给定容忍度的颜色.应该容易吗?
用于在图像中查找颜色的代码,并返回x,y:
def FindColorIn(r,g,b, xmin, xmax, ymin, ymax):
image = ImageGrab.grab()
for x in range(xmin, xmax):
for y in range(ymin,ymax):
px = image.getpixel((x, y))
if px[0] == r and px[1] == g and px[2] == b:
return x, y
def FindColor(r,g,b):
image = ImageGrab.grab()
size = image.size
pos = FindColorIn(r,g,b, 1, size[0], 1, size[1])
return pos
Run Code Online (Sandbox Code Playgroud)
结果:
从答案中得出,比较两种颜色的常规方法是欧几里德距离或切比雪夫距离.
我决定大多使用(平方)欧几里德距离和多个不同的颜色空间.LAB,deltaE(LCH),XYZ,HSL和RGB.在我的代码中,大多数颜色空间使用平方的欧氏距离来计算差异.
例如,LAB,RGB和XYZ是一个简单的平方euc.距离的诀窍是:
if ((X-X1)^2 + (Y-Y1)^2 + (Z-Z1)^2) <= (Tol^2) then
...
Run Code Online (Sandbox Code Playgroud)
LCH和HSL稍微复杂一点,因为它们都有圆柱形色调,但是有些数学解决了这个问题,那么它就是使用平方的eucl.这里也是.
在大多数情况下,我为每个通道添加了"单独的参数"(使用1个全局容差和替代"修饰符" HueTol := Tolerance * hueMod
或LightTol := …
我正在尝试编写一个可以在Python(2.7)中导入的DLL,并且我遇到了"使其工作"的困难.当我使用WinDLL()
或在Python中加载库时windll.LoadLibrary()
,并测试导出的函数,我得到的输出是空的.如果我向TestFunction()
它添加一个参数,则会引发一个ValueError
声明可能存在许多参数(实际上并非如此).
蟒蛇文件:
from ctypes import *
x = windll.LoadLibrary('./pymod.dll')
print x.TestFunction(123) #Should return 123.
Run Code Online (Sandbox Code Playgroud)
main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C"
{
#endif
int DLL_EXPORT TestFunction(int data);
#ifdef __cplusplus
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
和main.cpp:
#include "main.h"
int DLL_EXPORT TestFunction(int x = 0) {
return x;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch (fdwReason){
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH: …
Run Code Online (Sandbox Code Playgroud) 所以,我试图在Delphi中获得两个TPoints之间的角度,结果证明它比我预期的更难.我得到的结果我无法解释(似乎是"to degrees"-part的一些问题,或者ArcTan2没有以我期望的形式返回一个总和. - Delpi-v7:
function Modulo(x,y:Extended): Extended;
var d: Extended;
begin
d := x / y;
Result := (d - floor(d)) * y;
end;
function Degrees(Rads: Extended): Extended;
begin
Result := Rads*(180/Pi);
end;
function GetPointAngle(P1, P2: TPoint): Extended;
begin
Result := Modulo(Degrees(ArcTan2(-(P1.Y - P2.Y), P1.X - P2.X)) - 90, 360);
end;
Run Code Online (Sandbox Code Playgroud)
然而,当我将代码移植到Python或在另一个Pascal变体中测试它时,上面的工作.但现在,它似乎返回一个静态的总和(如果我"移动"第二个TPoint则不会改变).
如果你想知道; 我创建"模数"函数只是因为"mod" - 运算符中使用的除法运算符舍入为0而不是向下(因此负数不起作用).
编辑:我注意到,GetPointAngle()
当p
距离另一个点更远时,返回的值(角度)会增加c
(反之亦然),即使p
是沿着第二个TPoint(c
)的X轴拖动TPoint()也是如此.
编辑:
你们已经超越了自己,我已经查看了大部分答案,而且似乎很难选择最佳答案!既然你们都写了这么详细的东西,我会用相同的细节去做所有事情:-)
另外:我在最初的帖子中没有分享的是,我的函数被导出为DLL,可以从另一个pascal-interpretor(与delphi兼容)中访问.
最后的解决方案(已更改):
GetPointAngle(P1, P2: TPoint)
至: GetPointAngle(const …
我正在尝试制作一个简单的脚本,它将返回枚举的含义,让我们举一个例子://EG.class(应该返回动物ID).
import Object.Animal;
public class EG {
public void main() {
Animal AnimalID = Object.Animal.CAT;
System.out.print(AnimalID);
//Should return value of CAT: 2000 (long)
//But I can't figure out what's wrong.
}
}
Run Code Online (Sandbox Code Playgroud)
//Object.class
public class Object {
public enum Animal {
CAT(2000L), DOG(2001L), MONKEY(2002L), TIGER(2003L);
private long animal;
private Animal(long a) {
animal = a;
}
public long getAnimal() {
return animal;
}
}
}
Run Code Online (Sandbox Code Playgroud) 目前正在尝试将C用于以前在python(pypy)中完成的工作.我想我会尝试用C语言编写它(以获得最佳速度),并使用ctypes进行通信.
现在我想做的是从Bitmap(bmp文件)中获取pixelbuffer,将其发送到C函数,该函数将原始缓冲区转换为R,G,B值的平面数组并将其返回到python.但是当我试图将"缓冲区"转换为R,G,B值时,我陷入了困境.在python中,我只需使用"struct"模块:B,G,R = struct.unpack('<BBB', buffer[i:i+3])
我该如何在C中做同样的事情?
蟒蛇:
from bmplib import Bitmap
import ctypes
lib = ctypes.CDLL('_bitmap.dll')
bmp = Bitmap()
bmp.open('4x4.bmp')
buf = bmp._rawAsArray() #Returns a array.array() of char (raw pixel-data)
addr, count = buf.buffer_info()
lib.getData.argtypes = []
arr = ctypes.cast(addr, ctypes.POINTER(ctypes.c_char))
lib.getData(arr, count) #Does not return anything yet..
Run Code Online (Sandbox Code Playgroud)
C尝试转换像素失败:
#include <stdio.h>
void getData(char *, const int);
void getData(char * array, const int length) {
int i = 0;
while(i < length) {
/* ----- Clearly wrong as i got …
Run Code Online (Sandbox Code Playgroud)