参上了 matlab fileexchange 有人上传了高斯 dx,dy一阶导卷积代码
卷积核的计算我修改成了核元素绝对值求做分母 归一化
和halcon的 derivate_gauss算子的计算结果对别如下
还是不知道怎么做到两者结果一致.
测试图像:
我的:
halcon的:
获取两份图像的灰度值到数组并做对应位置灰度值差值比较:
我的:
[0.0, -0.0171127, -0.11751, -0.465375, -0.150412, 0.543371, 0.593301, -0.107628, -0.970025, -1.41046, -0.960299, -0.176343, -1.27435, -5.1756, -10.3727, -9.90589, 0.61225, 10.9554, 9.85048, 0.0, 0.0, 0.208356, 0.115327, -0.308402, -0.150885, 0.379145, 0.4111, -0.118863, -0.669842, -1.06148, -0.924356, -0.424523, -1.25557, -4.80264, -10.2093, -10.2431, 0.166427, 10.8936, 10.0133, 0.0, 0.0, 0.320794, 0.203955, -0.189211, -0.172259, 0.139072, 0.16295, -0.148289, -0.238421, -0.417654, -0.655456, -0.712105, -1.35891, -4.49443, -10.1022, -10.52, -0.144598, 10.9706, 10.2562, 0.0, 0.0, 0.113014, -0.0446385, -0.243567, -0.115624, 0.108654, 0.114228, -0.0554365, -0.0396674, -0.111725, -0.375975, -0.782387, -1.6152, -4.61284, -10.0419, -10.2006, 0.199951, 11.0875, 10.1699, 0.0, 0.0, 0.13843, 0.090423, -0.0464684, 0.0398678, 0.184565, 0.242966, 0.128786, -0.125661, -0.306733, -0.400467, -0.844742, -1.9809, -5.06256, -9.96049, -9.31551, 1.09789, 11.0924, 9.61384, 0.0, 0.0, 0.316735, 0.527218, 0.334401, 0.040415, 0.0290709, 0.204294, 0.0847642, -0.298032, -0.462267, -0.469856, -0.913691, -2.20446, -5.44862, -9.99578, -8.60683, 1.9778, 11.2732, 9.25204, 0.0, 0.0, 0.355216, 0.680552, 0.360028, -0.154394, -0.0761677, 0.153896, -0.0722301, -0.34628, -0.35859, -0.264544, -0.666337, -2.0332, -5.38378, -10.0376, -8.91902, 1.52949, 11.0683, 9.34834, 0.0, 0.0, 0.00813668, 0.145338, -0.0566561, -0.162529, 0.282673, 0.472511, -0.133688, -0.608853, -0.503001, -0.12297, -0.262847, -1.56996, -5.00265, -9.87109, -9.30654, 0.689996, 10.412, 9.23679, 0.0, 0.0, -0.265113, -0.223993, -0.324266, -0.209522, 0.407827, 0.603787, -0.164638, -0.752346, -0.594309, -0.145571, -0.148656, -1.33127, -4.74882, -9.61469, -9.0573, 0.715514, 10.0906, 8.95648, 0.0, 0.0, -0.0866533, 0.0589123, -0.304772, -0.498769, 0.23971, 0.621732, -0.162745, -0.693168, -0.415723, -0.119597, -0.310689, -1.28379, -4.28373, -9.1567, -9.06842, 0.412215, 9.82283, 8.88992, 0.0, 0.0, 0.0460112, 0.30525, -0.191774, -0.566615, 0.17624, 0.494347, -0.37077, -0.751868, -0.237939, -0.0175654, -0.458492, -1.26254, -3.86987, -8.78384, -9.21286, 0.00469169, 9.6488, 8.91869, 0.0, 0.0, -0.0730904, 0.239958, -0.0973189, -0.32265, 0.282875, 0.259192, -0.703655, -0.99835, -0.352403, 0.136638, -0.198985, -1.18563, -3.93559, -8.79819, -9.12675, 0.0979309, 9.81818, 9.03784, 0.0, 0.0, -0.284285, -0.0825046, -0.296485, -0.24846, 0.407888, 0.345745, -0.539637, -0.962332, -0.60297, 0.0135667, 0.0321525, -0.914516, -3.88131, -9.00191, -9.24455, 0.259426, 10.0775, 9.10933, 0.0, 0.0, -0.538959, -0.372508, -0.503989, -0.512134, 0.232151, 0.602174, 0.0352789, -0.550827, -0.608777, -0.201935, -0.178455, -0.972721, -3.86122, -9.12581, -9.26903, 0.519443, 10.2612, 9.01412, 0.0, 0.0, -0.796822, -0.537023, -0.34667, -0.351541, 0.264936, 0.629153, 0.169248, -0.279282, -0.427791, -0.265414, -0.422364, -1.24366, -3.89766, -8.97421, -9.30103, 0.325586, 10.1243, 9.06293, 0.0, 0.0, -0.696778, -0.517929, -0.164338, 0.136977, 0.668173, 0.573728, -0.0698864, -0.321661, -0.184646, 0.0436396, -0.197497, -1.2991, -4.0584, -8.91166, -9.31965, 0.230907, 10.1942, 9.30744, 0.0, 0.0, -0.19155, -0.0666285, -0.0027456, 0.246543, 0.705462, 0.479134, -0.180383, -0.473669, -0.115906, 0.436369, 0.341068, -1.02276, -4.1881, -8.70386, -8.16763, 1.89555, 11.457, 9.79718, 0.0, 0.0, 0.274354, 0.517338, 0.180516, -0.0587984, 0.289945, 0.493964, 0.101619, -0.361508, -0.143207, 0.369088, 0.320784, -0.831343, -3.57933, -7.04331, -4.92169, 5.52175, 13.7778, 10.5316, 0.0, 0.0, 0.631951, 0.882481, 0.138202, -0.640499, -0.420596, 0.36245, 0.442807, -0.0482875, -0.0997617, 0.0798276, -0.122345, -1.01099, -2.78475, -4.42388, -0.267066, 10.3771, 16.5177, 11.1477, 0.0, 0.0, 0.778102, 1.0003, 0.0543812, -0.979057, -0.868641, 0.135686, 0.521734, 0.130132, -0.0258657, -0.0458021, -0.384789, -1.2152, -2.51246, -3.10354, 2.19503, 12.9358, 17.8852, 11.3807, 0.0]
halcon的:
[0.0, 0.00286366, -0.0790541, -0.301582, -0.0956241, 0.361102, 0.392685, -0.0839792, -0.671474, -0.974847, -0.685607, -0.217952, -1.02633, -3.79819, -7.35155, -6.87377, 0.406537, 7.55653, 6.88076, 0.0, 0.0, 0.137308, 0.0645011, -0.20418, -0.0978744, 0.255895, 0.277404, -0.0874453, -0.480412, -0.746559, -0.651243, -0.361153, -1.01696, -3.58005, -7.2591, -7.07718, 0.141034, 7.51775, 6.97791, 0.0, 0.0, 0.206501, 0.124906, -0.128989, -0.112849, 0.0981371, 0.111574, -0.0947789, -0.185514, -0.317371, -0.471186, -0.536014, -1.08356, -3.38547, -7.18347, -7.2529, -0.0573025, 7.56126, 7.12873, 0.0, 0.0, 0.0820605, -0.0200196, -0.155739, -0.0771684, 0.0759264, 0.0824282, -0.0290115, -0.0420627, -0.104709, -0.291013, -0.588241, -1.25817, -3.46987, -7.13333, -7.02949, 0.181273, 7.63969, 7.07032, 0.0, 0.0, 0.0985283, 0.0710975, -0.0187323, 0.0260195, 0.123463, 0.164335, 0.0805792, -0.092918, -0.218767, -0.307969, -0.643596, -1.50833, -3.76341, -7.06849, -6.43466, 0.790312, 7.66888, 6.72876, 0.0, 0.0, 0.224165, 0.361002, 0.229101, 0.0321221, 0.0282326, 0.136068, 0.0489568, -0.203104, -0.320415, -0.349413, -0.686953, -1.66369, -4.01401, -7.07739, -5.97111, 1.35086, 7.78439, 6.50691, 0.0, 0.0, 0.245333, 0.447395, 0.240338, -0.0828919, -0.0319901, 0.109651, -0.0496896, -0.252779, -0.267651, -0.215417, -0.516831, -1.54228, -3.96865, -7.10486, -6.15985, 1.0698, 7.63436, 6.53821, 0.0, 0.0, 0.0142813, 0.0984331, -0.0319448, -0.0989224, 0.187414, 0.305131, -0.0914771, -0.418846, -0.353953, -0.115433, -0.245696, -1.22921, -3.71303, -7.00238, -6.41217, 0.528102, 7.2016, 6.44413, 0.0, 0.0, -0.16454, -0.14733, -0.219313, -0.142404, 0.273008, 0.401159, -0.109887, -0.515039, -0.414136, -0.121039, -0.160453, -1.05557, -3.51337, -6.81076, -6.27395, 0.491493, 6.96678, 6.25985, 0.0, 0.0, -0.0555199, 0.0194483, -0.214136, -0.320463, 0.165842, 0.404348, -0.11473, -0.479938, -0.303261, -0.104286, -0.256731, -1.00812, -3.1984, -6.50378, -6.26746, 0.294344, 6.78122, 6.2001, 0.0, 0.0, 0.0357159, 0.183683, -0.135095, -0.360271, 0.117133, 0.31096, -0.256551, -0.522923, -0.194121, -0.0348458, -0.334128, -0.984137, -2.92973, -6.25959, -6.34923, 0.0373512, 6.66277, 6.21462, 0.0, 0.0, -0.0391857, 0.139725, -0.0772477, -0.208043, 0.184323, 0.164836, -0.470848, -0.684792, -0.267337, 0.0581031, -0.170571, -0.917849, -2.94572, -6.26033, -6.3098, 0.0885262, 6.7697, 6.29478, 0.0, 0.0, -0.18779, -0.0769081, -0.207341, -0.170407, 0.25863, 0.226921, -0.360523, -0.661078, -0.420716, -0.0154985, -0.0281513, -0.749647, -2.91879, -6.38873, -6.3746, 0.208085, 6.94506, 6.34682, 0.0, 0.0, -0.365518, -0.280721, -0.349581, -0.328921, 0.161766, 0.395136, 0.0105877, -0.38955, -0.42101, -0.159048, -0.162161, -0.78775, -2.91071, -6.46208, -6.39081, 0.362532, 7.06126, 6.30277, 0.0, 0.0, -0.529335, -0.391131, -0.26082, -0.220412, 0.196724, 0.426461, 0.114726, -0.20395, -0.296473, -0.194275, -0.319435, -0.962622, -2.94092, -6.37876, -6.41441, 0.243902, 6.98251, 6.32988, 0.0, 0.0, -0.460975, -0.357795, -0.123462, 0.0969163, 0.449458, 0.395101, -0.0317531, -0.225538, -0.136575, 0.0177766, -0.165796, -0.992892, -3.04236, -6.34029, -6.38668, 0.242064, 7.07065, 6.49558, 0.0, 0.0, -0.129346, -0.048468, 0.00598212, 0.177542, 0.475248, 0.338683, -0.105016, -0.312601, -0.0803541, 0.278146, 0.180618, -0.810989, -3.08092, -6.11888, -5.53404, 1.42245, 7.96973, 6.86443, 0.0, 0.0, 0.197748, 0.346926, 0.127132, -0.0302968, 0.195883, 0.33379, 0.0727752, -0.231824, -0.091922, 0.241826, 0.179364, -0.673336, -2.63529, -4.90318, -3.22787, 3.99197, 9.63655, 7.41989, 0.0, 0.0, 0.441881, 0.598159, 0.0992456, -0.423639, -0.27906, 0.232817, 0.292092, -0.0219293, -0.0634963, 0.0498856, -0.110425, -0.767999, -2.04983, -3.05787, -0.0170716, 7.33341, 11.5758, 7.91567, 0.0, 0.0, 0.539733, 0.678084, 0.0436538, -0.649597, -0.568739, 0.0936762, 0.348525, 0.0935577, -0.0205136, -0.0372191, -0.282634, -0.885602, -1.82954, -2.13713, 1.64389, 9.05173, 12.5258, 8.1143, 0.0]
差值:
[0.0, 0.0199764, 0.0384561, 0.163793, 0.0547874, -0.182269, -0.200615, 0.0236485, 0.29855, 0.435614, 0.274692, -0.0416085, 0.248027, 1.37741, 3.02111, 3.03211, -0.205712, -3.39891, -2.96972, 0.0, 0.0, -0.0710479, -0.0508262, 0.104222, 0.0530104, -0.12325, -0.133696, 0.0314179, 0.18943, 0.314919, 0.273113, 0.0633705, 0.238615, 1.22259, 2.95016, 3.16593, -0.0253927, -3.37589, -3.0354, 0.0, 0.0, -0.114293, -0.0790493, 0.060222, 0.0594094, -0.0409349, -0.0513758, 0.0535105, 0.0529066, 0.100283, 0.184271, 0.176091, 0.275348, 1.10897, 2.91872, 3.2671, 0.0872955, -3.40938, -3.12742, 0.0, 0.0, -0.030953, 0.0246188, 0.0878278, 0.0384558, -0.0327278, -0.0317995, 0.026425, -0.00239531, 0.00701655, 0.0849619, 0.194146, 0.357037, 1.14298, 2.90858, 3.17111, -0.0186787, -3.4478, -3.0996, 0.0, 0.0, -0.0399018, -0.0193255, 0.0277361, -0.0138483, -0.0611017, -0.0786309, -0.0482067, 0.032743, 0.0879666, 0.0924983, 0.201146, 0.472566, 1.29915, 2.892, 2.88085, -0.307579, -3.42353, -2.88508, 0.0, 0.0, -0.0925702, -0.166215, -0.105301, -0.00829284, -0.00083828, -0.0682259, -0.0358073, 0.0949277, 0.141852, 0.120442, 0.226738, 0.540769, 1.43461, 2.91839, 2.63572, -0.626936, -3.48877, -2.74513, 0.0, 0.0, -0.109883, -0.233157, -0.11969, 0.0715022, 0.0441776, -0.0442449, 0.0225405, 0.0935012, 0.0909392, 0.0491273, 0.149506, 0.490917, 1.41513, 2.93278, 2.75917, -0.459694, -3.43394, -2.81013, 0.0, 0.0, 0.00614459, -0.0469047, 0.0247114, 0.0636062, -0.0952597, -0.16738, 0.0422108, 0.190006, 0.149047, 0.00753696, 0.0171512, 0.340754, 1.28962, 2.86871, 2.89438, -0.161894, -3.21041, -2.79266, 0.0, 0.0, 0.100573, 0.0766633, 0.104953, 0.067118, -0.134819, -0.202629, 0.0547512, 0.237307, 0.180173, 0.0245324, -0.0117976, 0.275699, 1.23545, 2.80393, 2.78335, -0.224021, -3.12386, -2.69662, 0.0, 0.0, 0.0311333, -0.039464, 0.0906356, 0.178306, -0.0738678, -0.217384, 0.0480156, 0.213229, 0.112461, 0.0153102, 0.0539579, 0.275674, 1.08533, 2.65292, 2.80095, -0.117871, -3.04161, -2.68982, 0.0, 0.0, -0.0102953, -0.121568, 0.0566782, 0.206344, -0.0591073, -0.183387, 0.11422, 0.228945, 0.0438185, -0.0172804, 0.124363, 0.278405, 0.940139, 2.52426, 2.86363, 0.0326595, -2.98603, -2.70407, 0.0, 0.0, 0.0339047, -0.100233, 0.0200711, 0.114607, -0.0985523, -0.0943557, 0.232807, 0.313558, 0.0850663, -0.0785351, 0.0284147, 0.267778, 0.989866, 2.53786, 2.81695, -0.00940474, -3.04848, -2.74307, 0.0, 0.0, 0.0964951, 0.00559648, 0.0891446, 0.0780527, -0.149257, -0.118824, 0.179114, 0.301254, 0.182254, -0.0290652, -0.0603038, 0.164868, 0.962518, 2.61318, 2.86995, -0.0513403, -3.13244, -2.7625, 0.0, 0.0, 0.173441, 0.0917867, 0.154408, 0.183213, -0.0703851, -0.207038, -0.0246913, 0.161277, 0.187766, 0.042887, 0.0162947, 0.184971, 0.950507, 2.66373, 2.87822, -0.156911, -3.19993, -2.71134, 0.0, 0.0, 0.267487, 0.145892, 0.08585, 0.131129, -0.0682123, -0.202693, -0.0545216, 0.0753321, 0.131318, 0.0711398, 0.10293, 0.28104, 0.956733, 2.59545, 2.88662, -0.0816842, -3.14178, -2.73305, 0.0, 0.0, 0.235803, 0.160134, 0.0408763, -0.0400609, -0.218715, -0.178627, 0.0381332, 0.0961231, 0.048071, -0.025863, 0.0317007, 0.306211, 1.01603, 2.57136, 2.93296, 0.0111569, -3.12352, -2.81186, 0.0, 0.0, 0.0622038, 0.0181605, 0.00872771, -0.069002, -0.230214, -0.140451, 0.0753671, 0.161067, 0.0355522, -0.158222, -0.16045, 0.211766, 1.10718, 2.58498, 2.63359, -0.473101, -3.48728, -2.93275, 0.0, 0.0, -0.0766059, -0.170413, -0.0533841, 0.0285016, -0.0940611, -0.160174, -0.0288434, 0.129683, 0.0512847, -0.127262, -0.14142, 0.158007, 0.944041, 2.14013, 1.69382, -1.52978, -4.14125, -3.11173, 0.0, 0.0, -0.19007, -0.284321, -0.038956, 0.21686, 0.141535, -0.129633, -0.150715, 0.0263583, 0.0362654, -0.029942, 0.0119208, 0.242992, 0.734925, 1.36601, 0.249995, -3.04374, -4.94188, -3.23203, 0.0, 0.0, -0.23837, -0.322216, -0.0107275, 0.329459, 0.299902, -0.0420101, -0.173209, -0.0365739, 0.00535204, 0.00858302, 0.102155, 0.329602, 0.68292, 0.966411, -0.551137, -3.88409, -5.35944, -3.26636, 0.0]
测试代码:
dev_close_window()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_window (WindowHandle)
read_image (Image, 'fabrik')
sigma:=1.0
kernel_size:=7
my_gen_derivative_filter (kernelX, 'x', sigma, kernel_size)
get_image_size (Image, Width, Height)
crop_part (Image, Image, Height/2-100, Width/2-10, 20, 20)
get_image_size (Image, Width, Height)
derivate_gauss (Image, ImageDx_halcon, 1, 'x')
gen_image_const (ImageDx_me, 'real',Width, Height)
half_size:=int(kernel_size/2)
for Row := -half_size to Height-1-half_size by 1
for Col := -half_size to Width-1-half_size by 1
GrayvalWeigthSum:=0
for Row1 := 0 to kernel_size-1 by 1
RowPixel:=Row+Row1
if(RowPixel<0)
RowPixel:=-RowPixel
elseif(RowPixel>=Height)
RowPixel:=Height+Height-RowPixel-2
endif
for Col1 := 0 to kernel_size-1 by 1
ColPixel:=Col+Col1
if(ColPixel<0)
ColPixel:=-ColPixel
elseif(ColPixel>=Width)
ColPixel:=Width+Width-ColPixel-2
endif
get_grayval(kernelX,Row1,Col1,kernel_Grayval)
get_grayval(Image,RowPixel,ColPixel,image_Grayval)
GrayvalWeigthSum:=GrayvalWeigthSum+kernel_Grayval*image_Grayval
endfor
endfor
set_grayval(ImageDx_me, Row+half_size, Col+half_size, GrayvalWeigthSum)
endfor
endfor
get_region_points(Image, Rows, Columns)
get_grayval(ImageDx_halcon, Rows, Columns, Grayval_ImageDx_halcon)
get_grayval(ImageDx_me, Rows, Columns, Grayval_ImageDx_me)
Grayval_ImageDx_diff:=Grayval_ImageDx_halcon-Grayval_ImageDx_me