├── .idea ├── PCNN-python.iml ├── misc.xml ├── modules.xml ├── vcs.xml └── workspace.xml ├── README.md ├── lena.jpg ├── main.py ├── noise.py ├── noise.pyc ├── pcnn.py └── tst.py /.idea/PCNN-python.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 72 | 73 | 74 | 76 | 77 | 85 | 86 | 87 | 88 | 89 | true 90 | DEFINITION_ORDER 91 | 92 | 93 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 130 | 131 | 134 | 135 | 138 | 139 | 140 | 141 | 144 | 145 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 179 | 180 | 199 | 200 | 201 | 202 | 203 | 216 | 217 | 230 | 231 | 248 | 249 | 261 | 262 | project 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 297 | 298 | 317 | 318 | 339 | 340 | 362 | 363 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 399 | 400 | 401 | 402 | 1474176334528 403 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 437 | 440 | 441 | 442 | 444 | 445 | 446 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PCNN-python 2 | 利用python学习PCNN,创建于20160912. 3 | 4 | 完成了对PCNN算法的点火过程,创建于20160930 5 | 6 | 整理代码,创建pcnn.py 20170623 7 | -------------------------------------------------------------------------------- /lena.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiayuqiu/PCNN-python/f78fe64e6644cddf45026bdb6510c0d6359b2dec/lena.jpg -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 # 2 | 3 | from pylab import * 4 | from scipy import signal as sg 5 | from noise import sp_noise 6 | 7 | import csv 8 | import numpy as np 9 | ##################################################################### 10 | 11 | class Pcnn_class(): 12 | 13 | def PCNN(self, img_arr, iteration_num, 14 | Af = 0.60, Al = 1.0, Atop = 0.80, 15 | Vf = 0.2, Vl = 0.2, Vtop = 2000.0, 16 | Beta = 0.1): 17 | # 获得矩阵的维度 18 | 19 | x_len, y_len = img_arr.shape 20 | 21 | # E = np.eye(x_len, dtype="float64") 22 | 23 | # 定义神经元矩阵,建立n=0时的F、L矩阵(都为0矩阵) 24 | 25 | W = np.array([[0.7070, 1, 0.7070], [1, 0, 1], [0.7070, 1, 0.7070]]) 26 | M = np.array([[0.7070, 1, 0.7070], [1, 0, 1], [0.7070, 1, 0.7070]]) 27 | Y = np.zeros_like(img_arr, dtype="float64") 28 | F = np.zeros_like(img_arr, dtype="float64") 29 | L = np.zeros_like(img_arr, dtype="float64") 30 | top = np.zeros_like(img_arr, dtype="float64") + 200.0 31 | 32 | # 定义点火过程 33 | temp = img_arr 34 | for i in range(0, iteration_num): 35 | 36 | K = sg.convolve2d(Y, M, mode="same") 37 | # print "第 %s 次迭代,K矩阵:" % i, "\n", K 38 | # raw_input("=================================") 39 | F = exp(-Af) * F + Vf * K 40 | L = exp(-Al) * L + Vl * K 41 | # print "第 %s 次迭代,L矩阵:" % i, "\n", L 42 | # raw_input("=================================") 43 | U = F * (1 + Beta * L) 44 | top = exp(-Atop) * top + Vtop * Y 45 | # print top 46 | # raw_input("top====================================") 47 | for x_axis in range(0, x_len): 48 | for y_axis in range(0, y_len): 49 | 50 | if (U[x_axis, y_axis] > top[x_axis, y_axis]): 51 | Y[x_axis, y_axis] = 1.0 52 | 53 | else: 54 | Y[x_axis, y_axis] = 0.0 55 | 56 | print len(Y[Y==1]) 57 | print "第 %s 次迭代完成。\n" % i 58 | 59 | # raw_input("=============================================") 60 | # print U 61 | return U 62 | 63 | ##################################################################### 64 | 65 | # 获得图像矩阵 66 | 67 | if __name__ == "__main__": 68 | 69 | # img = imread('/home/qiujiayu/图片/10_lena.jpg') 70 | csvfile = file('/home/qiujiayu/文档/3_lena.csv', 'rb') 71 | img = [] 72 | for line in csvfile: 73 | img.append(line.split(',')) 74 | 75 | img = [[float(y) for y in x]for x in img] 76 | img = np.array(img) 77 | 78 | noise_img = sp_noise(img, 0.05) 79 | # raw_input("================================") 80 | img_matlab = imread('/home/qiujiayu/图片/untitled.jpg') 81 | 82 | # 将图像矩阵归一化 83 | 84 | # img_normalized = np.array([[[float(rgb/255.0) for rgb in y_axis] for y_axis in x_axis] for x_axis in img]) 85 | # img_nor = np.array([[float(axis/255.0) for axis in line]for line in img]) 86 | 87 | p=Pcnn_class() 88 | img_out = p.PCNN(img_arr=img, iteration_num=30) 89 | # img_out = img_out * 255.0 90 | 91 | # print img_out 92 | # raw_input("===========================================") 93 | print "done!" 94 | 95 | gray() 96 | imshow(noise_img) 97 | show() 98 | 99 | -------------------------------------------------------------------------------- /noise.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | # import cv2 4 | 5 | def sp_noise(image,prob): 6 | 7 | output = np.zeros(image.shape,np.uint8) 8 | thres = 1 - prob 9 | for i in range(image.shape[0]): 10 | for j in range(image.shape[1]): 11 | rdn = random.random() 12 | if rdn < prob: 13 | output[i][j] = 0 14 | elif rdn > thres: 15 | output[i][j] = 255 16 | else: 17 | output[i][j] = image[i][j] 18 | return output 19 | 20 | # image = cv2.imread('image.jpg',0) # Only for grayscale image 21 | # noise_img = sp_noise(image,0.05) 22 | # cv2.imwrite('sp_noise.jpg', noise_img) -------------------------------------------------------------------------------- /noise.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiayuqiu/PCNN-python/f78fe64e6644cddf45026bdb6510c0d6359b2dec/noise.pyc -------------------------------------------------------------------------------- /pcnn.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | 3 | from pylab import * 4 | from scipy import signal as sg 5 | from noise import sp_noise 6 | 7 | ############################################################################################ 8 | class pcnn(object): 9 | """pcnn类""" 10 | 11 | #-------------------------------------------------------------------------------------- 12 | def __init__(self, iterationNum = 10): 13 | """初始化pcnn参数""" 14 | # 迭代次数 15 | self.iterationNum = iterationNum 16 | 17 | # 馈送输入的时间衰减常数 18 | self.Af = 0.60 19 | 20 | # 链接输入的时间衰减常数 21 | self.Al = 1.00 22 | 23 | # 动态门限的时间衰减常数 24 | self.Atop = 0.80 25 | 26 | # 馈送输入的放大系数 27 | self.Vf = 0.20 28 | 29 | # 链接输入的放大系数 30 | self.Vl = 0.20 31 | 32 | # 动态门限的放大系数 33 | self.Vtop = 2000. 34 | 35 | # 内部链接因子 36 | self.beta = 0.1 37 | 38 | # 动态门限初始值 39 | self.topInitValue = 200. 40 | 41 | # 神经元联系矩阵 42 | self.M = np.array([[0.7070, 1, 0.7070], [1, 0, 1], [0.7070, 1, 0.7070]]) 43 | 44 | #--------------------------------------------------------------------------------------- 45 | def initArr(self, imgArr): 46 | """初始化神经元矩阵""" 47 | # 脉冲矩阵 48 | Y = np.zeros_like(imgArr, dtype="float64") 49 | 50 | # 反馈输入矩阵 51 | F = np.zeros_like(imgArr, dtype="float64") 52 | 53 | # 耦合链接 54 | L = np.zeros_like(imgArr, dtype="float64") 55 | 56 | # 动态门限 57 | top = np.zeros_like(imgArr, dtype="float64") + self.topInitValue 58 | 59 | return [Y, F, L, top] 60 | 61 | #--------------------------------------------------------------------------------------- 62 | def fire(self, pmtArr): 63 | """pcnn点火过程""" 64 | 65 | x_len, y_len = pmtArr[0].shape 66 | for i in range(0, self.iterationNum): 67 | K = sg.convolve2d(pmtArr[0], self.M, mode="same") 68 | F = exp(-self.Af) * pmtArr[1] + self.Vf * K 69 | L = exp(-self.Al) * pmtArr[2] + self.Vl * K 70 | U = F * (1 + self.beta * L) 71 | top = exp(-self.Atop) * pmtArr[3] + self.Vtop * pmtArr[3] 72 | for x_axis in range(0, x_len): 73 | for y_axis in range(0, y_len): 74 | 75 | if (U[x_axis, y_axis] > top[x_axis, y_axis]): 76 | pmtArr[0][x_axis, y_axis] = 1.0 77 | 78 | else: 79 | pmtArr[0][x_axis, y_axis] = 0.0 80 | 81 | print "第 %s 次迭代完成。\n" % i 82 | return U 83 | 84 | #--------------------------------------------------------------------------------------- 85 | def pcnnMain(self, imgArr): 86 | """pcnn主函数 87 | 88 | 输入:imgArr - 图像矩阵 89 | iterationNum - 迭代次数,默认10次 90 | """ 91 | # 初始化神经元矩阵,并建立n=0时,F,L矩阵 92 | pass 93 | 94 | if __name__ == "__main__": 95 | print("hello world!") 96 | -------------------------------------------------------------------------------- /tst.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class cls(object): 4 | 5 | --------------------------------------------------------------------------------