Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.45 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  H3_2
  4. //
  5. //  Created by Ulrich Ziegler on 30.12.17.
  6. //  Copyright © 2017 Ulrich Ziegler. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15.  
  16. struct image{
  17.     int pixels[200];
  18.     int weights[200][200];
  19. };
  20.  
  21.  
  22. int exact_match = 0;
  23.  
  24.  
  25. void input(struct image image_input[10], struct image image_distorted[100], int* number_input, int* number_distorted){
  26.    
  27.     char input[20] = {0};
  28.     char lines[4] = {0};
  29.     int iterator_input = 0;
  30.     int iterator_distorted = 0;
  31.    
  32.     while(iterator_input < 10){
  33.         for(int i = 0; i < 10; i++){
  34.            
  35.             scanf("%s",input);
  36.            
  37.             for(int j = 0; j < 20; j++){
  38.                
  39.                 if(input[j]=='*'){
  40.                     image_input[iterator_input].pixels[i*20+j] = 1;
  41.                 }
  42.                
  43.                 if(input[j]=='.'){
  44.                     image_input[iterator_input].pixels[i*20+j] = -1;
  45.                 }
  46.             }
  47.         }
  48.         scanf("%s",lines);
  49.         iterator_input++;
  50.        
  51.         if(lines[0] == '-' && lines[1]== '-' && lines[2] == '-'){
  52.             *number_input = iterator_input;
  53.             break;
  54.         }
  55.     }
  56.    
  57.     int check = 0;
  58.    
  59.     while(check != EOF){
  60.         for(int i = 0; i < 10; i++){
  61.            
  62.             scanf("%s",input);
  63.            
  64.             for(int j = 0; j < 20; j++){
  65.                
  66.                 if(input[j]=='*'){
  67.                     image_distorted[iterator_distorted].pixels[i*20+j] = 1;
  68.                 }
  69.                 if(input[j]=='.'){
  70.                     image_distorted[iterator_distorted].pixels[i*20+j] = -1;
  71.                 }
  72.             }
  73.         }
  74.         check = scanf("%s",lines);
  75.         iterator_distorted++;
  76.     }
  77.     *number_distorted = iterator_distorted;
  78. }
  79.  
  80.  
  81.  
  82. int compare_pixels(struct image image_input[10], int number_input, int pix[200]){
  83.    
  84.     int best_match_image = 0;
  85.     int same_pixels_current;
  86.     int same_pixels_global = 0;
  87.    
  88.     for(int i = 0; i < number_input; i++){
  89.         same_pixels_current = 0;
  90.         for(int j = 0; j < 200; j++){
  91.             if(image_input[i].pixels[j] == pix[j]){
  92.                 same_pixels_current++;
  93.             }
  94.         }
  95.         if(same_pixels_current > same_pixels_global){
  96.             same_pixels_global = same_pixels_current;
  97.             best_match_image = i;
  98.         }
  99.         if(same_pixels_current > 199){
  100.             exact_match = 1;
  101.             break;
  102.         }
  103.     }
  104.     return best_match_image;
  105. }
  106.  
  107.  
  108.  
  109. void printer(struct image image_input[10], int index){
  110.     for(int i = 0; i < 10; i++){
  111.         for(int j = 0; j < 20; j++){
  112.             if(image_input[index].pixels[i*20+j] == 1){
  113.                 printf("%c", '*');
  114.             }
  115.             else{
  116.                 printf("%c", '.');
  117.             }
  118.         }
  119.         printf("\n");
  120.     }
  121. }
  122.  
  123.  
  124.  
  125. int main() {
  126.    
  127.     struct image image_input[10];
  128.     struct image image_distorted[10];
  129.     int number_input = 0;
  130.     int number_distorted = 0;
  131.    
  132.    
  133.     input(image_input, image_distorted, &number_input, &number_distorted);
  134.    
  135.    
  136.     for (int i = 0; i < number_input; i++)
  137.     {
  138.         for (int j = 0; j < 200; j++)
  139.         {
  140.             for (int k = 0; k < 200; k++)
  141.             {
  142.                 image_input[i].weights[j][k] = image_input[i].pixels[j] * image_input[i].pixels[k];
  143.             }
  144.             image_input[i].weights[j][j] = 0;
  145.         }
  146.     }
  147.    
  148.     int global_weights[200][200] = {{0}};
  149.    
  150.     for( int i = 0; i < number_input; i++)
  151.     {
  152.         for (int j = 0; j < 200; j++)
  153.         {
  154.             for (int k = 0; k < 200; k++)
  155.             {
  156.                 global_weights[j][k] += image_input[i].weights[j][k];
  157.             }
  158.         }
  159.     }
  160.    
  161.    
  162.    
  163.     //-----------------------------HOPFIELD ALGORITHM----------------------
  164.     int h[200];
  165.     int time_per_picture = 8/number_distorted;
  166.     int match_index = 0;
  167.    
  168.    
  169.     for (int i = 0; i < number_distorted; i++)
  170.     {
  171.         time_t start = time(NULL);
  172.        
  173.         while(1){
  174.            
  175.             for (int j = 0; j < 200; j++){
  176.                
  177.                 for (int k = 0; k < 200; k++){
  178.                     h[j] = h[j] + global_weights[j][k] * image_distorted[i].pixels[k];
  179.                 }
  180.                 if(h[j] >= 0){
  181.                     h[j] = 1;
  182.                 }
  183.                 else{
  184.                     h[j] = -1;
  185.                 }
  186.             }
  187.            
  188.             for(int l = 0; l < 200; l++){
  189.                 image_distorted[i].pixels[l] = h[l];
  190.             }
  191.            
  192.             match_index = compare_pixels(image_input, number_input, image_distorted[i].pixels);
  193.            
  194.             if(exact_match == 1){
  195.                 exact_match = 0;
  196.                 break;
  197.             }
  198.            
  199.             if(start-time(NULL) > time_per_picture){
  200.                 exact_match = 0;
  201.                 break;
  202.             }
  203.         }
  204.        
  205.        
  206.         match_index = compare_pixels(image_input, number_input, image_distorted[i].pixels);
  207.        
  208.         printer(image_input, match_index);
  209.         //match_index = 0;
  210.        
  211.         if(i < number_distorted - 1){
  212.             printf("-\n");
  213.             match_index = 0;
  214.             for(int m = 0; m < 200; m++){
  215.                 h[m] = 0;
  216.             }
  217.         }
  218.     }
  219.    
  220.     return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement