Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // H3_2
- //
- // Created by Ulrich Ziegler on 30.12.17.
- // Copyright © 2017 Ulrich Ziegler. All rights reserved.
- //
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- #include <time.h>
- struct image{
- int pixels[200];
- int weights[200][200];
- };
- int exact_match = 0;
- void input(struct image image_input[10], struct image image_distorted[100], int* number_input, int* number_distorted){
- char input[20] = {0};
- char lines[4] = {0};
- int iterator_input = 0;
- int iterator_distorted = 0;
- while(iterator_input < 10){
- for(int i = 0; i < 10; i++){
- scanf("%s",input);
- for(int j = 0; j < 20; j++){
- if(input[j]=='*'){
- image_input[iterator_input].pixels[i*20+j] = 1;
- }
- if(input[j]=='.'){
- image_input[iterator_input].pixels[i*20+j] = -1;
- }
- }
- }
- scanf("%s",lines);
- iterator_input++;
- if(lines[0] == '-' && lines[1]== '-' && lines[2] == '-'){
- *number_input = iterator_input;
- break;
- }
- }
- int check = 0;
- while(check != EOF){
- for(int i = 0; i < 10; i++){
- scanf("%s",input);
- for(int j = 0; j < 20; j++){
- if(input[j]=='*'){
- image_distorted[iterator_distorted].pixels[i*20+j] = 1;
- }
- if(input[j]=='.'){
- image_distorted[iterator_distorted].pixels[i*20+j] = -1;
- }
- }
- }
- check = scanf("%s",lines);
- iterator_distorted++;
- }
- *number_distorted = iterator_distorted;
- }
- int compare_pixels(struct image image_input[10], int number_input, int pix[200]){
- int best_match_image = 0;
- int same_pixels_current;
- int same_pixels_global = 0;
- for(int i = 0; i < number_input; i++){
- same_pixels_current = 0;
- for(int j = 0; j < 200; j++){
- if(image_input[i].pixels[j] == pix[j]){
- same_pixels_current++;
- }
- }
- if(same_pixels_current > same_pixels_global){
- same_pixels_global = same_pixels_current;
- best_match_image = i;
- }
- if(same_pixels_current > 199){
- exact_match = 1;
- break;
- }
- }
- return best_match_image;
- }
- void printer(struct image image_input[10], int index){
- for(int i = 0; i < 10; i++){
- for(int j = 0; j < 20; j++){
- if(image_input[index].pixels[i*20+j] == 1){
- printf("%c", '*');
- }
- else{
- printf("%c", '.');
- }
- }
- printf("\n");
- }
- }
- int main() {
- struct image image_input[10];
- struct image image_distorted[10];
- int number_input = 0;
- int number_distorted = 0;
- input(image_input, image_distorted, &number_input, &number_distorted);
- for (int i = 0; i < number_input; i++)
- {
- for (int j = 0; j < 200; j++)
- {
- for (int k = 0; k < 200; k++)
- {
- image_input[i].weights[j][k] = image_input[i].pixels[j] * image_input[i].pixels[k];
- }
- image_input[i].weights[j][j] = 0;
- }
- }
- int global_weights[200][200] = {{0}};
- for( int i = 0; i < number_input; i++)
- {
- for (int j = 0; j < 200; j++)
- {
- for (int k = 0; k < 200; k++)
- {
- global_weights[j][k] += image_input[i].weights[j][k];
- }
- }
- }
- //-----------------------------HOPFIELD ALGORITHM----------------------
- int h[200];
- int time_per_picture = 8/number_distorted;
- int match_index = 0;
- for (int i = 0; i < number_distorted; i++)
- {
- time_t start = time(NULL);
- while(1){
- for (int j = 0; j < 200; j++){
- for (int k = 0; k < 200; k++){
- h[j] = h[j] + global_weights[j][k] * image_distorted[i].pixels[k];
- }
- if(h[j] >= 0){
- h[j] = 1;
- }
- else{
- h[j] = -1;
- }
- }
- for(int l = 0; l < 200; l++){
- image_distorted[i].pixels[l] = h[l];
- }
- match_index = compare_pixels(image_input, number_input, image_distorted[i].pixels);
- if(exact_match == 1){
- exact_match = 0;
- break;
- }
- if(start-time(NULL) > time_per_picture){
- exact_match = 0;
- break;
- }
- }
- match_index = compare_pixels(image_input, number_input, image_distorted[i].pixels);
- printer(image_input, match_index);
- //match_index = 0;
- if(i < number_distorted - 1){
- printf("-\n");
- match_index = 0;
- for(int m = 0; m < 200; m++){
- h[m] = 0;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement