diff --git a/BackupGetBoundingBox.m b/BackupGetBoundingBox.m new file mode 100644 index 0000000..3467d2f --- /dev/null +++ b/BackupGetBoundingBox.m @@ -0,0 +1,222 @@ +% +% +%% BACKUP FOR GET BOUNDING BOX +% +% +% function [BoundingBoxes] = GetBoundingBoxes(CCs,CCstats,Features,NeedToStabilize,hasParametersSupplied,Parameters) +% +% BoundingBoxes = zeros(1,4); +% Features = abs(Features); +% % NOTE: The Feature Values supplied need to be of already stable +% % componenets if hasParametersSupplied is false. +% +% %% Features +% +% % 1. Lower Range Pixel Deviatiion ([0,1]) +% % 2. Higher Range Pixel Deviation ([0,1]) +% % 3. Lower Range change of Euler Number/100([0,1)) +% % 4. Higher Range change of Euler Number/100([0,1)) +% % 5. Lower Range Density Deviation ([0,1]) +% % 6. Higher Range Density Deviation ([0,1]) +% % 7. No. of Pixels [0,1) +% % 8. Height [0,1) +% % 9. Width [0,1) +% % 10. Solidity [0,1] +% % 11. Euler/100 [0,1) +% +% % 12 Eccentricity [0,1] +% % 13. Extent [0,1] +% % 14. SVT [0,1] +% % 15. eHOG [0,1] +% +% %% PARAMETERS +% +% % 1. Max Lower Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1 ) +% % 2. Max Higher Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1) +% % 3. Max Euler Number/100( Value > 1 and Value < Infinite ) +% % 4. Max Difference in Euler Number for Lower Range/100( 0 <= Value < 1 ) +% % 5. Max Difference in Euler Number for Higher Range/100( 0 <= Value < 1 ) +% % 6. Max Solidity ( Value > 0 and Value < 1) +% % 7. Min Solidity ( Value > 0 and Value < 1) +% % 8. Max Lower Range Density Deviation Allowed ( 0 < Value < 1 ) +% % 9. Max Higher Range Density Deviation Allowed ( 0 < Value < 1 ) +% % 10. Max No. of Pixels Ratio +% % 11. Min No. of Pixels Ratio +% % 12. Max Height Ratio +% % 13. Min Height Ratio +% % 14. Max Width Ratio +% % 15. Min Width Ratio +% +% +% % 16. Baseline Deviation by average height for aligned ( 0 < Value < 1 ) +% % 17. Spacing Deviation by average height for aligned (0 < value < 1 ) +% % 18. Height Difference by average height for aligned ( 0 < Value < 1 ) +% % 19. Maximum negative starting point by average height for aligned (0 < Value < 1) +% +% +% % 20. Max Average Solidity for aligned ( 0 < Value < 1) +% % 21. Min Average Solidity for aligned ( 0 < Value < Max Solidity) +% % 22. Max Average Euler Number for aligned ( 0 < Value < 1) +% % 23. Min Average Euler Number for aligned ( 0 < Value < Max Euler) +% % 24. Max Average Eccentricity for Aligned ( 0 < Value < 1) +% % 25. Min Average Eccentricity for Aligned ( 0 < Value < Max Eccentricity) +% % 26. Max Average Extent for aligned ( 0 < Value < 1) +% % 27. Min Average Extent for aligned ( 0 < Value < Max Extent) +% % 28. Max Average SVT for aligned ( 0 < Value < 1) +% % 29. Max Average eHOG for aligned ( 0 < Value < 1) +% +% % 30. Max Solidity [0,1] +% % 31. Min Solidity [0,Max Solidity] +% % 32. Max Euler Number [0,1) +% % 33. Min Euler Number [0,Max Euler) +% % 34. Max Eccentricity [0,1] +% % 35. Min Eccentricity [0,Max Eccentricity] +% % 36. Max Extent [0,1] +% % 37. Min Extent [0, Max Extent) +% % 38. Max SVT [0,1] +% % 39. Max eHOG [0,1] +% +% % data = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.25 0.5 0.25 0.5 +% % 0.25 0.25 0.25 0.25 0.25 0.8 0.1 0.06 0 0.98 0 0.9 0 0.25 0.3 +% % 0.65 0.1 0.06 0 0.99 0 0.9 0.2 0.25 0.3] +% %% Parameter Initialization +% +% if hasParametersSupplied +% GroupingParams = Parameters(1,16:19); % PARAMETER ARRAY DEPENDENT +% AlignedGroupParams_MAX = [Parameters(1,20:2:26) Parameters(1,28:29)]; +% AlignedGroupParams_MIN = [Parameters(1,21:2:27) 0 0]; +% AlignedGroupParams_MAX = AlignedGroupParams_MAX + AlignedGroupParams_MIN; +% NonAlignedGroupParams_MAX = [Parameters(1,30:2:36) Parameters(1,38:39)]; +% NonAlignedGroupParams_MIN = [Parameters(1,31:2:37) 0 0]; +% NonAlignedGroupParams_MAX = NonAlignedGroupParams_MAX + NonAlignedGroupParams_MIN; +% else % DEFAULT VALUES +% GroupingParams = [ 0.25 0.25 0.25 0.25 ]; +% AlignedGroupParams_MAX = [ 0.8 0.06 0.98 0.9 0.25 0.3]; +% AlignedGroupParams_MIN = [0.1 0 0 0.2 0 0]; +% NonAlignedGroupParams_MAX = [0.65 0.06 0.99 0.9 0.25 0.3]; +% NonAlignedGroupParams_MIN = [ 0.1 0 0 0.2 0 0]; +% end +% +% +% %% CODE +% start = 1; +% numBBs = 0; +% component_class = zeros(size(Features,1),1); % Vector Denoting Class +% % 0 - Undecided +% % 1 - Non Text +% % 2 - Text +% % 3 - Stable Component +% +% for i = 1:size(CCs,2) +% if CCs(1,i) == 0 +% continue; +% end +% ends = CCs(1,i); +% +% for comp = start:ends +% if component_class(comp,1) == 1 || component_class(comp,1) == 2 %If already classified,skip over +% continue +% end +% +% if NeedToStabilize && component_class(comp,1) == 0 +% if hasParametersSupplied +% [isStable] = PredictStabilityFromParameters(Features(comp,1:11),Parameters(1,1:15)); +% else +% error('Need to supply Parameters if Unstable components supplied'); +% end +% +% if ~isStable +% component_class(comp,1) = 1; +% continue +% else +% component_class(comp,1) = 3; +% end +% end % Stabilization done +% +% +% stats = CCstats(comp,:); +% +% avg_height = stats(1,4); +% avg_baseline = stats(1,2) + stats(1,4); +% max_y = stats(1,1) + stats(1,3); +% max_x = stats(1,2) + stats(1,4); +% aligned_comps = zeros(1,20); % An Array of aligned comp labels, BUFFER OF UPTO 20 ALIGNED COMPONENTS +% aligned_comps(1,:) = comp; +% aligns = 1; +% sum_features = Features(comp,10:15); +% +% BB = stats(:,:); +% +% for scan_comp = (comp+1):ends +% +% if component_class(scan_comp,1) ~= 0 && component_class(scan_comp,1) ~= 3 +% continue; +% end +% +% if NeedToStabilize && component_class(scan_comp,1) == 0 +% if hasParametersSupplied +% [isStable] = PredictStabilityFromParameters(Features(scan_comp,1:11),Parameters(1,1:15)); +% else +% error('Need to supply Parameters if Unstable components supplied'); +% end +% +% if ~isStable +% component_class(scan_comp,1) = 1; +% continue +% else +% component_class(scan_comp,1) = 3; +% end +% end %Stabilization done +% +% +% scan_stats = CCstats(scan_comp,:); +% spacing_dev = abs(scan_stats(1,1) - max_y)/avg_height; +% if spacing_dev > 1.25*GroupingParams(1,2) % CRITICAL ASSUME: Once components reach a certain distance horizontally,it keeps on increasing +% break; +% end +% baseline_dev = abs(scan_stats(1,2) + scan_stats(1,4) - avg_baseline)/avg_height; +% height_dev = abs(scan_stats(1,4) - avg_height)/avg_height; +% startPoint_diff = (max_y - scan_stats(1,1))/avg_height; +% +% C_Arr = [baseline_dev spacing_dev height_dev startPoint_diff]; +% isAligned = C_Arr <= GroupingParams; +% +% if isAligned == 1 +% +% avg_height = (avg_height*aligns + scan_stats(1,4))/(aligns+1); +% avg_baseline = (avg_baseline*aligns + (scan_stats(1,2) + scan_stats(1,4)))/(aligns+1); +% max_y = max(max_y,scan_stats(1,1)+scan_stats(1,3)); +% max_x = max(max_x,scan_stats(1,2)+scan_stats(1,4)); +% aligns = aligns + 1; +% aligned_comps(1,aligns) = scan_comp; +% sum_features = sum_features + Features(scan_comp,10:15); +% +% BB(1,1) = min(BB(1,1),scan_stats(1,1)); %%Increasing the Bounding Box +% BB(1,3) = max_y - BB(1,1); +% BB(1,2) = min(BB(1,2),scan_stats(1,2)); +% BB(1,4) = max_x - BB(1,2); +% end +% +% end +% +% C_Arr = sum_features./aligns; +% +% if aligns > 1 +% isTextGroup = (C_Arr <= AlignedGroupParams_MAX) & (C_Arr >= AlignedGroupParams_MIN); +% else +% isTextGroup = (C_Arr <= NonAlignedGroupParams_MAX ) & ( C_Arr >= NonAlignedGroupParams_MIN ); +% end +% +% if isTextGroup == 1 +% numBBs = numBBs + 1; +% BoundingBoxes(numBBs,:) = BB; +% component_class(aligned_comps,1) = 2; +% else +% component_class(aligned_comps,1) = 1; +% end +% +% end +% start = ends + 1; +% end +% +% end \ No newline at end of file diff --git a/BinImages2.mat b/BinImages2.mat new file mode 100644 index 0000000..0df6f29 Binary files /dev/null and b/BinImages2.mat differ diff --git a/Binning.m b/Binning.m index d686ecd..b337b61 100644 --- a/Binning.m +++ b/Binning.m @@ -1,12 +1,11 @@ -function [BinImages,NUM_BIN_IMAGES,MAX_DISTANCE] = Binning( image,BinSizes) - +function [NUM_BIN_IMAGES,MAX_DISTANCE] = Binning( image,BinSizes) +global BinImages %% This function Bins the images based on BinSizes and returns a set of Binary Images(:BinSizes) ,the number of such Images(:BUM_BIN_IMAGES) % And the maximum value of a pixel(for grey scale it's 255) %CODE image = histeq(image); - MAX_DISTANCE = 255; NUM_BIN_IMAGES = calculateNumBins_2Level(BinSizes,MAX_DISTANCE); @@ -17,37 +16,52 @@ BinImages = false(row,col,NUM_BIN_IMAGES); - +BinMatrix = GetBinAllocations(BinSizes,MAX_DISTANCE,NUM_BIN_IMAGES); q_offset = 0; DistanceMatrix = zeros(row,col); for r = 1:row for c = 1:col + if size(image,3) == 1 - DistanceMatrix(r,c) = image(r,c); %%ASSUMED GRAY SCALE IMAGE,OTHERWISE VALUE ARE RGB Euclidean distance + DistanceMatrix(r,c) = double(image(r,c)); %%ASSUMED GRAY SCALE IMAGE,OTHERWISE VALUE ARE RGB Euclidean distance else - DistanceMatrix(r,c) = image(r,c); + DistanceMatrix(r,c) = double(image(r,c)); end end end -for i = 1:numel(BinSizes) - main_offset = ceil(MAX_DISTANCE/BinSizes(i)); - k = ceil((BinSizes(i)/2)) -1; +for i = 1:size(BinSizes,2) + + main_offset = ceil(MAX_DISTANCE/BinSizes(1,i)); + k = ceil((BinSizes(1,i)/2)) -1; % fprintf("\nMapping Image to Bin Size: %d",BinSizes(i)); for r = 1:row for c = 1:col value = DistanceMatrix(r,c); - x = q_offset+floor(value/BinSizes(i))+1; + x = q_offset+floor(value/BinSizes(1,i))+1; +% if x > size(BinImages,3) +% error('Wrong calculation of NumberBinImages'); +% end BinImages(r,c,x) = 1; +% if BinMatrix(x,3) > value || BinMatrix(x,4) < value +% warning("ERROR:: "); +% fprintf("\nERROR; VALUES ARE NOT MAPPING TO BIN_MATRIX for pixel value= %d , Mapped to Main Bin = %d\n",value,x); +% end - if value>k && value<(((main_offset-1)*BinSizes(i))+k) %%CHANGES IN MAX DISTANCE MADE - y = q_offset+main_offset+ceil((value-k)/BinSizes(i)); + if value>k && value<(((main_offset-1)*BinSizes(1,i))+k) %%CHANGES IN MAX DISTANCE MADE + y = q_offset+main_offset+ceil((value-k)/BinSizes(1,i)); +% if y > size(BinImages,3) +% error('Wrong calculation of NumberBinImages'); +% end BinImages(r,c,y) = 1; +% if BinMatrix(y,3) > value || BinMatrix(y,4) < value +% fprintf("\nERROR; VALUES ARE NOT MAPPING TO BIN_MATRIX for pixel value= %d , Mapped to Offset Bin = %d, k = %d Main_offset*BinSizes = %d\n",value,y,k,(((main_offset-1)*BinSizes(i))+k+1)); +% end end end diff --git a/BinningGL.m b/BinningGL.m index 4f05bd8..89550b2 100644 --- a/BinningGL.m +++ b/BinningGL.m @@ -1,47 +1,127 @@ function [fitness] = BinningGL(chromosome) global img ExtractionDone BinNumber FCCs FCCs_indexes FCCstats FCCstats_indexes FFeatures FFeatures_indexes; -%folderPath='with GT/'; -folderPath = 'E:\ResearchFiles\DATA\test_input\'; -idir = dir(strcat(folderPath,'*.png')); -nfiles = uint16(length(idir)*(1.0/100.0)); % Number of files found %OVERWRITTEN -nfiles = length({idir.name}); +folderPath='with GT/'; +%folderPath = 'E:\ResearchFiles\DATA\Scene-Text-Recognition-GIAN\with GT\'; % Path on Indra's Machine only +idir = dir(strcat(folderPath,'i (*).jpg')); +nfiles = 10; accuracy = zeros(1,nfiles); -%initialiseGT(); +initialiseGT(); + +loadSavedVariables = true; + +if ~ExtractionDone && loadSavedVariables + load('FCCs.mat'); + load('FCCs_indexes.mat'); + load('FCCstats.mat'); + load('FCCstats_indexes.mat'); + load('FFeatures.mat'); + load('FFeatures_indexes.mat'); + load('ExtractedFileNames.mat'); + load('NumFilesExtracted.mat'); + tFCCs= FCCs ; + tFCCstats = FCCstats; + tFFeatures = FFeatures; + entryCC = numel(FCCs)+1; + entryCCst = numel(FCCstats)+1; + entryF = size(FFeatures,1)+1; +else + if ~ExtractionDone + NumFilesExtracted = 0; + ExtractedFileNames = strings(nfiles,1); + entryCC = 1; + entryCCst = 1; + entryF = 1; + FCCstats_indexes = zeros(nfiles,2); + FFeatures_indexes = zeros(nfiles,2); + FCCs_indexes = zeros(nfiles,2); + + end +end + + if ExtractionDone == false - entryCC = 1; - entryCCst = 1; - entryF = 1; - FCCstats_indexes = zeros(nfiles,2); - FFeatures_indexes = zeros(nfiles,2); + index_entry = NumFilesExtracted+1; + tFCCinitialized = false; for img_loop=1:nfiles + % + % try fprintf(">>>> Extracting Features for Image : %d\n",img_loop); currentfilename = idir(img_loop).name; + hasBeenDone = (ExtractedFileNames == string(currentfilename)); + if sum(hasBeenDone) ~= 0 + fprintf("File Already Extracted\n"); + continue; + end + imagePath=strcat(folderPath,currentfilename); img = rgb2gray(imread(imagePath)); image = img; - BinSizes = [32,47,62,76,90,103,116]; + BinSizes = [32,50,62,77,95,118]; - [BinImages,NumBinImages,MAX_DISTANCE] = Binning(image,BinSizes); + fprintf(".....Binning Image.....\n"); + [NumBinImages,MAX_DISTANCE] = Binning(image,BinSizes); BinNumber = NumBinImages; BinMatrix = GetBinAllocations(BinSizes,MAX_DISTANCE,NumBinImages); + if ~tFCCinitialized + tFCCs = zeros(nfiles,NumBinImages); + tFCCstats = zeros(nfiles,4); + tFCCinitialized = true; + end + StabilityMatrix = GetStabilityMatrix(BinSizes,BinMatrix,MAX_DISTANCE); - [CCs,CCstats,Features,~] = GetAllFeatures(ReduceToMainCCs(BinImages),BinSizes,MAX_DISTANCE,StabilityMatrix,false) - - tFCCs(entryCC:entryCC+numel(CCs)-1) = CCs; - FCCs_indexes(img_loop,:) = [entryCC entryCC+numel(CCs)-1]; - entryCC = entryCC + numel(CCs); - tFCCstats(entryCCst:entryCCst+numel(CCstats)-1) = CCstats; - FCCstats_indexes(img_loop,:) = [entryCCst entryCCst+numel(CCstats)-1]; - entryCCst = entryCCst + numel(CCstats); + fprintf("......Extracting Features........\n"); + [CCs,CCstats,Features,~] = GetAllFeatures(BinSizes,MAX_DISTANCE,StabilityMatrix,false); + %CCs + tFCCs(entryCC:entryCC+size(CCs,1)-1,:) = CCs; + FCCs_indexes(index_entry,:) = [entryCC (entryCC+size(CCs,1)-1)]; + entryCC = entryCC + size(CCs,1); + tFCCstats(entryCCst:entryCCst+size(CCstats,1)-1,:) = CCstats; + FCCstats_indexes(index_entry,:) = [entryCCst entryCCst+size(CCstats,1)-1]; + entryCCst = entryCCst + size(CCstats,1); tFFeatures(entryF:entryF+size(Features,1)-1,:) = Features; - FFeatures_indexes(img_loop,:) = [entryF entryF+size(Features,1)-1]; + FFeatures_indexes(index_entry,:) = [entryF entryF+size(Features,1)-1]; entryF = entryF+size(Features,1); + + index_entry = index_entry +1; + + NumFilesExtracted = NumFilesExtracted + 1; + ExtractedFileNames(NumFilesExtracted,1) = string(currentfilename); + % catch e + % fprintf("Error:: \n %s \n %s ",e.identifier,e.message); + % FCCs = tFCCs; + % FCCstats = tFCCstats; + % FFeatures = tFFeatures; + % save('FCCs.mat','FCCs'); + % save('FCCs_indexes.mat','FCCs_indexes'); + % save('FCCstats.mat','FCCstats'); + % save('FCCstats_indexes.mat','FCCstats_indexes'); + % save('FFeatures.mat','FFeatures'); + % save('FFeatures_indexes.mat','FFeatures_indexes'); + % save('ExtractedFileNames.mat','ExtractedFileNames'); + % save('NumFilesExtracted.mat','NumFilesExtracted'); + % ExtractionDone = true; + % error("THERE HAS BEEN ERROR"); + % end end - FCCs = tFCCs; - FCCstats = tFCCstats; - FFeatures = tFFeatures; + + try + FCCs = tFCCs; + FCCstats = tFCCstats; + FFeatures = tFFeatures; + catch e + fprintf("All Images in this folder already Feature extraction completed\n"); + fprintf("%s %s\n",e.identifier,e.message); + end + save('FCCs.mat','FCCs'); + save('FCCs_indexes.mat','FCCs_indexes'); + save('FCCstats.mat','FCCstats'); + save('FCCstats_indexes.mat','FCCstats_indexes'); + save('FFeatures.mat','FFeatures'); + save('FFeatures_indexes.mat','FFeatures_indexes'); + save('ExtractedFileNames.mat','ExtractedFileNames'); + save('NumFilesExtracted.mat','NumFilesExtracted'); ExtractionDone = true; end @@ -51,18 +131,18 @@ img = rgb2gray(imread(imagePath)); textBoxes = boundingBoxes(chromosome,img_loop); - + %disp(textBoxes); accuracy(img_loop) = overlapAccuracy(textBoxes, currentfilename); - fprintf('Accuracy ratio - %f\n',accuracy(img_loop)); + %fprintf('Accuracy ratio - %f\n',accuracy(img_loop)); end -disp(accuracy); +%disp(accuracy); fitness = mean(accuracy); end function [BoundingBoxes] = boundingBoxes(parameters,img_no) % returns a list of tex boxes for all bin sizes -global FCCs FCCs_indexes FCCstats FCCstats_indexes FFeatures FFeatures_indexes; +global FCCs FCCs_indexes FCCstats FCCstats_indexes FFeatures FFeatures_indexes CheckTextGroup CheckisStable; hasParametersSupplied = true; startCC = FCCs_indexes(img_no,1); @@ -74,12 +154,21 @@ startFeatures = FFeatures_indexes(img_no,1); endFeatures = FFeatures_indexes(img_no,2); -CCs = FCCs(startCC:endCC) -CCstats = FCCstats(startCCstats:endCCstats) -Features = FFeatures(startFeatures:endFeatures,:) +% FFeatures = normalize(FFeatures,1,'range'); +% FFeatures(:,7) = FFeatures(:,7)*10; + %FFeatures(:,15) = FFeatures(:,11).*10; +CCs = FCCs(startCC:endCC,:); +CCstats = FCCstats(startCCstats:endCCstats,:); +Features = FFeatures(startFeatures:endFeatures,:); + Features(:,[3 4 11]) = Features(:,[3 4 11])./1000; +% Features(:,[8 9]) = Features(:,[8,9])*10; +CheckTextGroup = zeros(1,6); +CheckisStable = zeros(1,11); BoundingBoxes = GetBoundingBoxes(CCs,CCstats,Features,true,hasParametersSupplied,parameters); +%disp(CheckisStable); +%disp(CheckTextGroup); end @@ -96,23 +185,42 @@ function [accuracy] = overlapAccuracy(textBoxes, currentfilename) global img list gtBoundingbox; [row,col] = size(img); - +% disp(size(img)); mask = zeros(row,col); +mask2 = zeros(row,col); +% size(mask2) textBoxes = uint16(textBoxes); % mark the GT in mask for i = 1:size(gtBoundingbox,2) if strcmp(gtBoundingbox{i},currentfilename) - mask(list(i,1):list(i,1)+list(i,3)-1,list(i,2):list(i,2)+list(i,4)-1) = 1; + mask(list(i,2):list(i,2)+list(i,4)-1,list(i,1):list(i,1)+list(i,3)-1) = 1; end end % mark output in mask +% may have put x ad y wrong, check it +% fprintf('groud truth only - %d\n',sum(mask(:)==1)); for i =1:size(textBoxes,1) xmin = max(textBoxes(i,1),1); - xmax = min(textBoxes(i,1)+textBoxes(i,3)-1,row); + xmax = min(textBoxes(i,1)+textBoxes(i,3)-1,col); ymin = max(textBoxes(i,2),1); - ymax = min(textBoxes(i,2)+textBoxes(i,4)-1,col); - mask(xmin:xmax,ymin:ymax) = mask(xmin:xmax,ymin:ymax) + 2; + ymax = min(textBoxes(i,2)+textBoxes(i,4)-1,row); + + %INDRA's modification + mask2(ymin:ymax, xmin:xmax) = mask2(ymin:ymax, xmin:xmax)+2; + % fprintf('text box at - (%d, %d) to (%d, %d)\n',xmin,ymin,xmax,ymax); + % disp(size(mask2)); +end +% disp(size(mask)); +% disp(size(mask2)); + + + +mask = mask + mask2; +textBoxOnly = sum(sum(mask(mask ~= 0 & mod(mask,2) ~= 1)./2)); +intersection = sum(sum((mask(mod(mask,2) == 1)-1)./2)); +union = textBoxOnly + intersection + sum(sum(mask == 1)); +%fprintf('textBox only - %d : intersection - %d : union - %d\n',textBoxOnly,intersection,union); +%fprintf('textBox only - %d : intersection - %d : union - %d\n',sum(mask(:)==2),sum(mask(:)==3),sum(mask(:)~=0)); +%accuracy = sum(mask(:)==3)/(sum(mask(:)~=0)); +accuracy = intersection/union; end -fprintf('textBox only - %d : intersection - %d : union - %d\n',sum(mask(:)==2),sum(mask(:)==3),sum(mask(:)~=0)); -accuracy = sum(mask(:)==3)/(sum(mask(:)~=0)); -end \ No newline at end of file diff --git a/ExtractedFileNames.mat b/ExtractedFileNames.mat new file mode 100644 index 0000000..72a107f Binary files /dev/null and b/ExtractedFileNames.mat differ diff --git a/FCCs.mat b/FCCs.mat new file mode 100644 index 0000000..6a216f9 Binary files /dev/null and b/FCCs.mat differ diff --git a/FCCs_indexes.mat b/FCCs_indexes.mat new file mode 100644 index 0000000..351f6eb Binary files /dev/null and b/FCCs_indexes.mat differ diff --git a/FCCstats.mat b/FCCstats.mat new file mode 100644 index 0000000..235406c Binary files /dev/null and b/FCCstats.mat differ diff --git a/FCCstats_indexes.mat b/FCCstats_indexes.mat new file mode 100644 index 0000000..44ff8ae Binary files /dev/null and b/FCCstats_indexes.mat differ diff --git a/FFeatures.mat b/FFeatures.mat new file mode 100644 index 0000000..8f90853 Binary files /dev/null and b/FFeatures.mat differ diff --git a/FFeatures_indexes.mat b/FFeatures_indexes.mat new file mode 100644 index 0000000..a44766e Binary files /dev/null and b/FFeatures_indexes.mat differ diff --git a/GetAllFeatures.m b/GetAllFeatures.m index f3f08e8..59c5356 100644 --- a/GetAllFeatures.m +++ b/GetAllFeatures.m @@ -1,389 +1,470 @@ -function [CCs,CCstats,Features,FinalBinImages] = GetAllFeatures(BinImages,BinSizes,MAX_DISTANCE,StabilityCheckMatrix,getFinalBinImages) % Make 2nd one false for speed - +function [PageEndings,statsBoxes,Features,FinalBinImages] = GetAllFeatures(BinSizes,MAX_DISTANCE,StabilityCheckMatrix,getFinalBinImages) % Make 2nd one false for speed +global BinImages %ShowOutput %% Features Extracted - % 1. Lower Range Pixel Deviatiion ([0,Inf]) - % 2. Higher Range Pixel Deviation ([0,Inf]) - % 3. Lower Range change of Euler Number/100([0,Inf)) - % 4. Higher Range change of Euler Number/100([0,Inf)) - % 5. Lower Range Density Deviation ([0,Inf]) - % 6. Higher Range Density Deviation ([0,Inf]) - % 7. No. of Pixels/MaxPixels [0,Inf) - % 8. Height/MaxHeight (1,Inf) - % 9. Width/MaxWidth (1,Inf) - % 10. Solidity [0,1] - % 11. Euler/100 [0,Inf) - - % 12 Eccentricity [0,1] - % 13. Extent [0,1] - % 14. SVT [0,1] - % 15. eHOG [0,1] +% 1. Lower Range Pixel Deviatiion ([0,Inf]) +% 2. Higher Range Pixel Deviation ([0,Inf]) +% 3. Lower Range change of Euler Number/100([0,Inf)) +% 4. Higher Range change of Euler Number/100([0,Inf)) +% 5. Lower Range Density Deviation ([0,Inf]) +% 6. Higher Range Density Deviation ([0,Inf]) +% 7. No. of Pixels/MaxPixels [0,Inf) +% 8. Height/MaxHeight (1,Inf) +% 9. Width/MaxWidth (1,Inf) +% 10. Solidity [0,1] +% 11. Euler/100 [0,Inf) + +% 12 Eccentricity [0,1] +% 13. Extent [0,1] +% 14. SVT [0,1] +% 15. eHOG [0,1] + + - Features = zeros(1,15); - %% CODE - show_error = false; %% CHANGE TO TRUE TO SHOW ERRORS +show_error = true; %% CHANGE TO TRUE TO SHOW ERRORS [row,col,NUM_BIN_IMAGES] = size(BinImages); +Features = zeros(1,15); +PageEndings = zeros(1,NUM_BIN_IMAGES); +prev=0; +statsBoxes = zeros(1,4); + cc_no = 1; -ccs_en = 1; - palate = zeros(row,col); - - if getFinalBinImages + + + +if getFinalBinImages FinalBinImages = zeros(size(BinImages)); - else - FinalBinImages = zeros(1,1); % NA IF FINAL BIN IMAGES NOT WANTED - end - - - - -q_offset = 0; +else + FinalBinImages = zeros(1,1); % NA IF FINAL BIN IMAGES NOT WANTED +end - for i = 1:numel(BinSizes) %Must change Loop for change in Bin - - if i~=1 && StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset+1),1) == 0 - fprintf('GAP:: i = %d has a checking bin but before that does not',i); - continue; - end - main_offset = ceil(MAX_DISTANCE/BinSizes(i)); - k = ceil((BinSizes(i)/2)) -1; - - if StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset),1) == 0 - fprintf('ALL IMAGES UNDER same I not equal'); - continue; + +%% PREPROCESSING +scan_imgs = false(row,col,NUM_BIN_IMAGES); +lower_range_check_imgs = false(row,col,NUM_BIN_IMAGES); +upper_range_check_imgs = false(row,col,NUM_BIN_IMAGES); +lower_range_bwimages = zeros(row,col,NUM_BIN_IMAGES); +upper_range_bwimages = zeros(row,col,NUM_BIN_IMAGES); +fprintf(".......preprocessing.........\n"); +q_offset = 0; +for i = 1:numel(BinSizes) + main_offset = ceil(MAX_DISTANCE/BinSizes(i)); + parfor img_no = (q_offset+1):(q_offset+main_offset) + + if img_no ~= (q_offset+main_offset) + scan_imgs(:,:,img_no) = ReduceToMainCCs(logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no+main_offset)))); + else + scan_imgs(:,:,img_no) = ReduceToMainCCs(logical(BinImages(:,:,img_no))); end - - %For 1st Level Bins - for img_no = (q_offset+1):(q_offset+main_offset) - close all - % fprintf('Processing Bin No.: %d\n',img_no); - + if StabilityCheckMatrix(img_no,1) == 0 + continue; + end + if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+1,img_no-1),1) == 0 + StabilityCheckMatrix + error('ALL IMAGES UNDER same I not equal'); + end + lower_overlap_bin_no = img_no + main_offset-1; + upper_overlap_bin_no = img_no+main_offset; + if((img_no > NUM_BIN_IMAGES) || ((img_no+main_offset)> NUM_BIN_IMAGES && img_no~=q_offset+main_offset)) + fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); + end - if img_no ~= (q_offset+main_offset) - scan_img = logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no+main_offset))); + %The Smaller range against which to check stability + if img_no > q_offset+1 && img_no<(q_offset+main_offset) + lower_range_check_imgs(:,:,img_no) = logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no)); else - scan_img = logical(BinImages(:,:,img_no)); + if img_no == q_offset+main_offset + lower_range_check_imgs(:,:,img_no) = logical(BinImages(:,:,img_no)+ BinImages(:,:,lower_overlap_bin_no)); + else + lower_range_check_imgs(:,:,img_no) = logical(BinImages(:,:,img_no)+BinImages(:,:,upper_overlap_bin_no)); + end end - CC_scan_img = bwconncomp(scan_img); + lower_range_check_imgs(:,:,img_no) = ReduceToMainCCs(lower_range_check_imgs(:,:,img_no)); + + upper_range_check_img_no_1= StabilityCheckMatrix(img_no,6); + upper_range_check_img_no_2= StabilityCheckMatrix(img_no,7); + + + + upper_range_check_imgs(:,:,img_no) = logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2)); + upper_range_check_imgs(:,:,img_no) = ReduceToMainCCs(upper_range_check_imgs(:,:,img_no)); + + lower_range_bwimages(:,:,img_no) = bwlabel(lower_range_check_imgs(:,:,img_no)); + upper_range_bwimages(:,:,img_no) = bwlabel(upper_range_check_imgs(:,:,img_no)); + end + + parfor img_no = (q_offset+main_offset+1):(q_offset+2*main_offset-1) + + scan_imgs(:,:,img_no) = ReduceToMainCCs(logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no-main_offset+1)))); if StabilityCheckMatrix(img_no,1) == 0 - continue; + continue; end - if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+1,img_no-1),1) == 0 + + if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+main_offset+1,img_no-1),1) == 0 fprintf('ALL IMAGES UNDER same I not equal'); - continue; + continue; end + %The Smaller range against which to check stability - if CC_scan_img.NumObjects == 0 - continue; + lower_overlap_bin_no = img_no -main_offset; + upper_overlap_bin_no = img_no-main_offset+1; + if((img_no > NUM_BIN_IMAGES) || (lower_overlap_bin_no> NUM_BIN_IMAGES)) + fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); end -% label_scan_img = bwlabel(scan_img); - - CCs(ccs_en) = CC_scan_img; - ccs_en = ccs_en+1; + lower_range_check_imgs(:,:,img_no) = ReduceToMainCCs(logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no))); - lower_overlap_bin_no = img_no + main_offset-1; - upper_overlap_bin_no = img_no+main_offset; - if((img_no > NUM_BIN_IMAGES) || ((img_no+main_offset)> NUM_BIN_IMAGES && img_no~=q_offset+main_offset)) - - fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); - - end - %The Smaller range against which to check stability - if img_no > q_offset+1 && img_no<(q_offset+main_offset) - lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no)); - else - if img_no == q_offset+main_offset - lower_range_check_img = logical(BinImages(:,:,img_no)+ BinImages(:,:,lower_overlap_bin_no)); - else - lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,upper_overlap_bin_no)); - end - end - lower_range_check_CC = bwconncomp(lower_range_check_img); - - upper_range_check_img_no_1= StabilityCheckMatrix(img_no,6); upper_range_check_img_no_2= StabilityCheckMatrix(img_no,7); + + + upper_range_check_imgs(:,:,img_no) = ReduceToMainCCs(logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2))); + + lower_range_bwimages(:,:,img_no) = bwlabel(lower_range_check_imgs(:,:,img_no)); + upper_range_bwimages(:,:,img_no) = bwlabel(upper_range_check_imgs(:,:,img_no)); + + end + + q_offset = q_offset + 2*main_offset - 1; +end +%% EXTRACTING +q_offset = 0; +fprintf("....extracting..........\n\n"); - upper_range_check_img = logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2)); - upper_range_check_CC = bwconncomp(upper_range_check_img); - - lower_range_bwimage = bwlabel(lower_range_check_img); - upper_range_bwimage = bwlabel(upper_range_check_img); - - stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent'); - - lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity'); - upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity'); - for comp = 1:CC_scan_img.NumObjects - FeatureValues(:,:) = 0; - lower_range_overlap_comp = findLabels(lower_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); %Make to 1 for speed - upper_range_overlap_comp = findLabels(upper_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); - - if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0) - if show_error - fprintf('\n Wrong Calculation in LOWER Range Check Image'); - figure('Name','ERROR:No Overlap Component !! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image'); - error_figure(:,:) = 0; - imshow(lower_range_check_img); - end - continue; - end + + +for i = 1:numel(BinSizes) %Must change Loop for change in Bin + + if i~=1 && StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset+1),1) == 0 + StabilityCheckMatrix + error('GAP:: i = %d has a checking bin but before that does not',i); + end + main_offset = ceil(MAX_DISTANCE/BinSizes(i)); + k = ceil((BinSizes(i)/2)) -1; + + if StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset),1) == 0 + StabilityCheckMatrix + error('ALL IMAGES UNDER same I not equal'); + end + + %For 1st Level Bins + for img_no = (q_offset+1):(q_offset+main_offset) - if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 ) - if show_error - fprintf('\n Wrong Calculation in LOWER Range Check Image'); - figure('Name','ERROR:No Overlap Component!! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image'); - error_figure(:,:) = 0; - imshow(upper_range_check_img); - end - continue; + scan_img = scan_imgs(:,:,img_no); + CC_scan_img = bwconncomp(scan_img); + + + if StabilityCheckMatrix(img_no,1) == 0 + continue; end + if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+1,img_no-1),1) == 0 + StabilityCheckMatrix + error('ALL IMAGES UNDER same I not equal'); + end + + PageEndings(1,img_no) = prev + CC_scan_img.NumObjects; + prev = PageEndings(1,img_no); - comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp}); - - upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}); - lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}); - - if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels) - fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced '); - figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image'); - error_figure(:,:) = 0; - imshow(lower_range_check_img); - figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component'); - error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1; - imshow(error_figure); - error_figure(:,:) = 0; - continue; + if CC_scan_img.NumObjects == 0 + continue; end + - if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels) - fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced '); - figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image'); - error_figure(:,:) = 0; - imshow(upper_range_check_img); - figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component'); - error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1; - imshow(error_figure); - error_figure(:,:) = 0; - continue; + + + if((img_no > NUM_BIN_IMAGES) || ((img_no+main_offset)> NUM_BIN_IMAGES && img_no~=q_offset+main_offset)) + fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); end + %The Smaller range against which to check stability + + lower_range_check_img = lower_range_check_imgs(:,:,img_no); + + lower_range_check_CC = bwconncomp(lower_range_check_img); + + + upper_range_check_img = upper_range_check_imgs(:,:,img_no); + upper_range_check_CC = bwconncomp(upper_range_check_img); + + lower_range_bwimage = lower_range_bwimages(:,:,img_no); + upper_range_bwimage = upper_range_bwimages(:,:,img_no); + + stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent'); + + + lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity'); + upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity'); + CompFeatureValues = zeros(CC_scan_img.NumObjects,15); + CompstatsBoxes = zeros(CC_scan_img.NumObjects,4); + parfor comp = 1:CC_scan_img.NumObjects - - FeatureValues(1,1) = (abs(numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); - FeatureValues(1,2) = (abs(numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); - FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; - FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; - FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; - FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; - FeatureValues(1,7) = numel(CC_scan_img.PixelIdxList{comp})/(row*col); - FeatureValues(1,8) = stats(comp).BoundingBox(4)/row; - FeatureValues(1,9) = stats(comp).BoundingBox(3)/col; - - palate(CC_scan_img.PixelIdxList{comp}) = 1; - - FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ]; - palate(:,:) = 0; - - - - - - Features(cc_no,:) = FeatureValues; - try - CCstats(cc_no) = stats(comp); - catch - fprintf('\nimg_en: %d\n',cc_no); - end - cc_no = cc_no + 1; + FeatureValues = zeros(1,15); + + + region = CC_scan_img.PixelIdxList{comp}; + lower_range_overlap_comp = lower_range_bwimage(region(1)); %Make to 1 for speed + upper_range_overlap_comp = upper_range_bwimage(region(1)); + + % if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0) + % if show_error + % fprintf('\n Wrong Calculation in LOWER Range Check Image in MAIN Loop for img_no = %d, i = %d',img_no,i); + % % figure('Name','ERROR:No Overlap Component !! K-means Component being scanned'); + % % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % % imshow(error_figure); + % % figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image'); + % % error_figure(:,:) = 0; + % % imshow(lower_range_check_img); + % end + % %count_errors = count_errors +1; + % continue; + % end + % + % if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 ) + % if show_error + % fprintf('\n Wrong Calculation in UPPER Range Check Image in MAIN LOOP for img_no = %d, i = %d',img_no,i); + % % figure('Name','ERROR:No Overlap Component!!Component being scanned'); + % % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % % imshow(error_figure); + % % figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image'); + % % error_figure(:,:) = 0; + % % imshow(upper_range_check_img); + % end + % % count_errors = count_errors +1; + % continue; + % end + + comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp}); + + upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}); + lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}); + + if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels) + fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced '); + % figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % imshow(error_figure); + % figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image'); + % error_figure(:,:) = 0; + % imshow(lower_range_check_img); + % figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component'); + % error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1; + % imshow(error_figure); + % error_figure(:,:) = 0; + continue; + end + + if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels) + fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced '); + % figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % imshow(error_figure); + % figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image'); + % error_figure(:,:) = 0; + % imshow(upper_range_check_img); + % figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component'); + % error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1; + % imshow(error_figure); + % error_figure(:,:) = 0; + continue; + end + + + FeatureValues(1,1) = (abs(lower_range_comp_no_pixels - comp_num_of_pixels))/comp_num_of_pixels; + FeatureValues(1,2) = (abs(upper_range_comp_no_pixels - comp_num_of_pixels))/comp_num_of_pixels; + FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,7) = comp_num_of_pixels/(row*col); + FeatureValues(1,8) = stats(comp).BoundingBox(4)/row; + FeatureValues(1,9) = stats(comp).BoundingBox(3)/col; + + palate = zeros(row,col); + palate(CC_scan_img.PixelIdxList{comp}) = 1; + + FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ]; + + + CompFeatureValues(comp,:) = FeatureValues; + CompstatsBoxes(comp,:) = stats(comp).BoundingBox; + + end + Features(cc_no:cc_no+size(CompFeatureValues,1)-1,:) = CompFeatureValues; + statsBoxes(cc_no:cc_no+size(CompFeatureValues,1)-1,:) = CompstatsBoxes; + cc_no = cc_no+size(CompFeatureValues,1); - + if getFinalBinImages + FinalBinImages(:,:,img_no) = scan_img; + end end - if getFinalBinImages - FinalBinImages(:,:,img_no) = scan_img; - end - - end - - - % For 2nd Level Bins - for img_no = (q_offset+main_offset+1):(q_offset+2*main_offset-1) - % fprintf("Processing Bin No: %d\n",img_no); - close all - - scan_img = logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no-main_offset+1))); + % For 2nd Level Bins + for img_no = (q_offset+main_offset+1):(q_offset+2*main_offset-1) + + scan_img = scan_imgs(:,:,img_no); CC_scan_img = bwconncomp(scan_img); - + if StabilityCheckMatrix(img_no,1) == 0 - continue; + continue; end - - if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+main_offset+1,img_no-1),1) == 0 - fprintf('ALL IMAGES UNDER same I not equal'); - continue; + if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+1,img_no-1),1) == 0 + StabilityCheckMatrix + error('ALL IMAGES UNDER same I not equal'); end + + PageEndings(1,img_no) = prev + CC_scan_img.NumObjects; + prev = PageEndings(1,img_no); + if CC_scan_img.NumObjects == 0 continue; end - -% label_scan_img = bwlabel(scan_img); - CCs(ccs_en) = CC_scan_img; - ccs_en = ccs_en+1; - - %The Smaller range against which to check stability - - lower_overlap_bin_no = img_no -main_offset; - upper_overlap_bin_no = img_no-main_offset+1; - if((img_no > NUM_BIN_IMAGES) || (lower_overlap_bin_no> NUM_BIN_IMAGES)) - fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); - end - lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no)); - - lower_range_check_CC = bwconncomp(lower_range_check_img); - - upper_range_check_img_no_1= StabilityCheckMatrix(img_no,6); - upper_range_check_img_no_2= StabilityCheckMatrix(img_no,7); - - - upper_range_check_img = logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2)); - upper_range_check_CC = bwconncomp(upper_range_check_img); - - lower_range_bwimage = bwlabel(lower_range_check_img); - upper_range_bwimage = bwlabel(upper_range_check_img); - - stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent'); - - - lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity'); - upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity'); - for comp = 1:CC_scan_img.NumObjects + - lower_range_overlap_comp = findLabels(lower_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); %Make to 1 for speed - upper_range_overlap_comp = findLabels(upper_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); - - if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0 ) - if show_error - fprintf('\n Wrong Calculation in LOWER Range Check Image'); - figure('Name','ERROR:No Overlap Component !! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image'); - error_figure(:,:) = 0; - imshow(lower_range_check_img); - end - continue; + + + if((img_no > NUM_BIN_IMAGES) || ((img_no+main_offset)> NUM_BIN_IMAGES && img_no~=q_offset+main_offset)) + fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); end + %The Smaller range against which to check stability + + lower_range_check_img = lower_range_check_imgs(:,:,img_no); + + lower_range_check_CC = bwconncomp(lower_range_check_img); - if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 ) - if show_error - fprintf('\n Wrong Calculation in LOWER Range Check Image'); - figure('Name','ERROR:No Overlap Component!! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image'); - error_figure(:,:) = 0; - imshow(upper_range_check_img); + + upper_range_check_img = upper_range_check_imgs(:,:,img_no); + upper_range_check_CC = bwconncomp(upper_range_check_img); + + lower_range_bwimage = lower_range_bwimages(:,:,img_no); + upper_range_bwimage = upper_range_bwimages(:,:,img_no); + + stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent'); + + + lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity'); + upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity'); + CompFeatureValues = zeros(CC_scan_img.NumObjects,15); + CompstatsBoxes = zeros(CC_scan_img.NumObjects,4); + + parfor comp = 1:CC_scan_img.NumObjects + FeatureValues = zeros(1,15); + + + region = CC_scan_img.PixelIdxList{comp}; + lower_range_overlap_comp = lower_range_bwimage(region(1)); %Make to 1 for speed + upper_range_overlap_comp = upper_range_bwimage(region(1)); + + % if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0) + % if show_error + % fprintf('\n Wrong Calculation in LOWER Range Check Image in MAIN Loop for img_no = %d, i = %d',img_no,i); + % % figure('Name','ERROR:No Overlap Component !! K-means Component being scanned'); + % % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % % imshow(error_figure); + % % figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image'); + % % error_figure(:,:) = 0; + % % imshow(lower_range_check_img); + % end + % %count_errors = count_errors +1; + % continue; + % end + % + % if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 ) + % if show_error + % fprintf('\n Wrong Calculation in UPPER Range Check Image in MAIN LOOP for img_no = %d, i = %d',img_no,i); + % % figure('Name','ERROR:No Overlap Component!!Component being scanned'); + % % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % % imshow(error_figure); + % % figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image'); + % % error_figure(:,:) = 0; + % % imshow(upper_range_check_img); + % end + % % count_errors = count_errors +1; + % continue; + % end + + comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp}); + + upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}); + lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}); + + if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels) + fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced '); + % figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % imshow(error_figure); + % figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image'); + % error_figure(:,:) = 0; + % imshow(lower_range_check_img); + % figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component'); + % error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1; + % imshow(error_figure); + % error_figure(:,:) = 0; + continue; + end + + if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels) + fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced '); + % figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + % error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + % imshow(error_figure); + % figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image'); + % error_figure(:,:) = 0; + % imshow(upper_range_check_img); + % figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component'); + % error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1; + % imshow(error_figure); + % error_figure(:,:) = 0; + continue; end - continue; + + + FeatureValues(1,1) = (abs(lower_range_comp_no_pixels - comp_num_of_pixels))/comp_num_of_pixels; + FeatureValues(1,2) = (abs(upper_range_comp_no_pixels - comp_num_of_pixels))/comp_num_of_pixels; + FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,7) = comp_num_of_pixels/(row*col); + FeatureValues(1,8) = stats(comp).BoundingBox(4)/row; + FeatureValues(1,9) = stats(comp).BoundingBox(3)/col; + + palate = zeros(row,col); + palate(CC_scan_img.PixelIdxList{comp}) = 1; + + FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ]; + + + CompFeatureValues(comp,:) = FeatureValues; + CompstatsBoxes(comp,:) = stats(comp).BoundingBox; + end + Features(cc_no:cc_no+size(CompFeatureValues,1)-1,:) = CompFeatureValues; + statsBoxes(cc_no:cc_no+size(CompFeatureValues,1)-1,:) = CompstatsBoxes; + cc_no = cc_no+size(CompFeatureValues,1); - comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp}); - - upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}); - lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}); - - if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels) - fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced '); - figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image'); - error_figure(:,:) = 0; - imshow(lower_range_check_img); - figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component'); - error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1; - imshow(error_figure); - error_figure(:,:) = 0; - continue; - end - - if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels) - fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced '); - figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); - error_figure(CC_scan_img.PixelIdxList{comp}) = 1; - imshow(error_figure); - figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image'); - error_figure(:,:) = 0; - imshow(upper_range_check_img); - figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component'); - error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1; - imshow(error_figure); - error_figure(:,:) = 0; - continue; + if getFinalBinImages + FinalBinImages(:,:,img_no) = scan_img; end - FeatureValues(1,1) = (abs(numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); - FeatureValues(1,2) = (abs(numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); - FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; - FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; - FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; - FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; - FeatureValues(1,7) = numel(CC_scan_img.PixelIdxList{comp})/(row*col); - FeatureValues(1,8) = stats(comp).BoundingBox(4)/row; - FeatureValues(1,9) = stats(comp).BoundingBox(3)/col; - - palate(CC_scan_img.PixelIdxList{comp}) = 1; - - FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ]; - palate(:,:) = 0; - - - Features(cc_no,:) = FeatureValues; - try - CCstats(cc_no) = stats(comp); - catch - fprintf('\nimg_en: %d\n',cc_no); - end - cc_no = cc_no + 1; - - end - - if getFinalBinImages - FinalBinImages(:,:,img_no) = scan_img; - end - end - - q_offset = q_offset+2*main_offset-1; - end - - - - + + q_offset = q_offset+2*main_offset-1; +end +% fprintf("..To be removed...\n"); +% PageEndings +% +% ShowOutput(:,:,1:2:end) = scan_imgs; end \ No newline at end of file diff --git a/GetAllFeatures.m~ b/GetAllFeatures.m~ new file mode 100644 index 0000000..b87582d --- /dev/null +++ b/GetAllFeatures.m~ @@ -0,0 +1,386 @@ +function [CCs,CCstats,Features,FinalBinImages] = GetAllFeatures(BinSizes,MAX_DISTANCE,StabilityCheckMatrix,getFinalBinImages) % Make 2nd one false for speed + +global BinImages +%% Features Extracted + + % 1. Lower Range Pixel Deviatiion ([0,Inf]) + % 2. Higher Range Pixel Deviation ([0,Inf]) + % 3. Lower Range change of Euler Number/100([0,Inf)) + % 4. Higher Range change of Euler Number/100([0,Inf)) + % 5. Lower Range Density Deviation ([0,Inf]) + % 6. Higher Range Density Deviation ([0,Inf]) + % 7. No. of Pixels/MaxPixels [0,Inf) + % 8. Height/MaxHeight (1,Inf) + % 9. Width/MaxWidth (1,Inf) + % 10. Solidity [0,1] + % 11. Euler/100 [0,Inf) + + % 12 Eccentricity [0,1] + % 13. Extent [0,1] + % 14. SVT [0,1] + % 15. eHOG [0,1] + + Features = zeros(1,15); + + +%% CODE + show_error = false; %% CHANGE TO TRUE TO SHOW ERRORS +[row,col,NUM_BIN_IMAGES] = size(BinImages); + +cc_no = 1; +ccs_en = 1; + palate = zeros(row,col); + + if getFinalBinImages + FinalBinImages = zeros(size(BinImages)); + else + FinalBinImages = zeros(1,1); % NA IF FINAL BIN IMAGES NOT WANTED + end + + + + +q_offset = 0; + + for i = 1:numel(BinSizes) %Must change Loop for change in Bin + + if i~=1 && StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset+1),1) == 0 + fprintf('GAP:: i = %d has a checking bin but before that does not',i); + continue; + end + main_offset = ceil(MAX_DISTANCE/BinSizes(i)); + k = ceil((BinSizes(i)/2)) -1; + + if StabilityCheckMatrix(q_offset+1,1) ~= 0 && StabilityCheckMatrix(max(1,q_offset-2*main_offset),1) == 0 + fprintf('ALL IMAGES UNDER same I not equal'); + continue; + end + + %For 1st Level Bins + for img_no = (q_offset+1):(q_offset+main_offset) + close all + % fprintf('Processing Bin No.: %d\n',img_no); + + + + + if img_no ~= (q_offset+main_offset) + scan_img = logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no+main_offset))); + else + scan_img = logical(BinImages(:,:,img_no)); + end + CC_scan_img = bwconncomp(scan_img); + + + if StabilityCheckMatrix(img_no,1) == 0 + continue; + end + if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+1,img_no-1),1) == 0 + fprintf('ALL IMAGES UNDER same I not equal'); + continue; + end + + + if CC_scan_img.NumObjects == 0 + continue; + end +% label_scan_img = bwlabel(scan_img); + + CCs(ccs_en) = CC_scan_img; + ccs_en = ccs_en+1; + + + lower_overlap_bin_no = img_no + main_offset-1; + upper_overlap_bin_no = img_no+main_offset; + if((img_no > NUM_BIN_IMAGES) || ((img_no+main_offset)> NUM_BIN_IMAGES && img_no~=q_offset+main_offset)) + + fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); + + end + %The Smaller range against which to check stability + if img_no > q_offset+1 && img_no<(q_offset+main_offset) + lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no)); + else + if img_no == q_offset+main_offset + lower_range_check_img = logical(BinImages(:,:,img_no)+ BinImages(:,:,lower_overlap_bin_no)); + else + lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,upper_overlap_bin_no)); + end + end + lower_range_check_CC = bwconncomp(lower_range_check_img); + + + upper_range_check_img_no_1= StabilityCheckMatrix(img_no,6); + upper_range_check_img_no_2= StabilityCheckMatrix(img_no,7); + + + upper_range_check_img = logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2)); + upper_range_check_CC = bwconncomp(upper_range_check_img); + + lower_range_bwimage = bwlabel(lower_range_check_img); + upper_range_bwimage = bwlabel(upper_range_check_img); + + stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent'); + + + lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity'); + upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity'); + CC_scan_img.NumObjects + count_errors = 0; + for comp = 1:CC_scan_img.NumObjects + FeatureValues(:,:) = 0; + CCstats(cc_no) = stats(comp); + cc_no = cc_no +1; + lower_range_overlap_comp = findLabels(lower_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); %Make to 1 for speed + upper_range_overlap_comp = findLabels(upper_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); + + if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0) + if show_error + fprintf('\n Wrong Calculation in LOWER Range Check Image'); + figure('Name','ERROR:No Overlap Component !! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image'); + error_figure(:,:) = 0; + imshow(lower_range_check_img); + end + count_errors = count_errors +1; + continue; + end + + if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 ) + if show_error + fprintf('\n Wrong Calculation in LOWER Range Check Image'); + figure('Name','ERROR:No Overlap Component!! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image'); + error_figure(:,:) = 0; + imshow(upper_range_check_img); + end + count_errors = count_errors +1; + continue; + end + + comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp}); + + upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}); + lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}); + + if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels) + fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced '); + figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image'); + error_figure(:,:) = 0; + imshow(lower_range_check_img); + figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component'); + error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1; + imshow(error_figure); + error_figure(:,:) = 0; + continue; + end + + if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels) + fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced '); + figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image'); + error_figure(:,:) = 0; + imshow(upper_range_check_img); + figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component'); + error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1; + imshow(error_figure); + error_figure(:,:) = 0; + continue; + end + + + FeatureValues(1,1) = (abs(numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); + FeatureValues(1,2) = (abs(numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); + FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,7) = numel(CC_scan_img.PixelIdxList{comp})/(row*col); + FeatureValues(1,8) = stats(comp).BoundingBox(4)/row; + FeatureValues(1,9) = stats(comp).BoundingBox(3)/col; + + palate(CC_scan_img.PixelIdxList{comp}) = 1; + + FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ]; + palate(:,:) = 0; + + + + + + Features(cc_no,:) = FeatureValues; + + + + + + end + + + if getFinalBinImages + FinalBinImages(:,:,img_no) = scan_img; + end + + end + + + % For 2nd Level Bins + for img_no = (q_offset+main_offset+1):(q_offset+2*main_offset-1) + % fprintf("Processing Bin No: %d\n",img_no); + close all + + scan_img = logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no-main_offset+1))); + CC_scan_img = bwconncomp(scan_img); + + + if StabilityCheckMatrix(img_no,1) == 0 + continue; + end + + if StabilityCheckMatrix(img_no,1) ~= 0 && StabilityCheckMatrix(max(q_offset+main_offset+1,img_no-1),1) == 0 + fprintf('ALL IMAGES UNDER same I not equal'); + continue; + end + if CC_scan_img.NumObjects == 0 + continue; + end + +% label_scan_img = bwlabel(scan_img); + CCs(ccs_en) = CC_scan_img; + ccs_en = ccs_en+1; + + %The Smaller range against which to check stability + + lower_overlap_bin_no = img_no -main_offset; + upper_overlap_bin_no = img_no-main_offset+1; + if((img_no > NUM_BIN_IMAGES) || (lower_overlap_bin_no> NUM_BIN_IMAGES)) + fprintf('\nLoop Error,img_value >134;printing loop at Bin Index: %d main_offset = %d ,q_offset = %d\n' ,main_offset,q_offset); + end + lower_range_check_img = logical(BinImages(:,:,img_no)+BinImages(:,:,lower_overlap_bin_no) + BinImages(:,:,upper_overlap_bin_no)); + + lower_range_check_CC = bwconncomp(lower_range_check_img); + + upper_range_check_img_no_1= StabilityCheckMatrix(img_no,6); + upper_range_check_img_no_2= StabilityCheckMatrix(img_no,7); + + + upper_range_check_img = logical(BinImages(:,:,upper_range_check_img_no_1)+BinImages(:,:,upper_range_check_img_no_2)); + upper_range_check_CC = bwconncomp(upper_range_check_img); + + lower_range_bwimage = bwlabel(lower_range_check_img); + upper_range_bwimage = bwlabel(upper_range_check_img); + + stats = regionprops(CC_scan_img,'EulerNumber','Solidity','BoundingBox', 'Eccentricity', 'Extent'); + + + lower_check_stats = regionprops(lower_range_check_img,'EulerNumber','Solidity'); + upper_check_stats = regionprops(upper_range_check_img,'EulerNumber','Solidity'); + for comp = 1:CC_scan_img.NumObjects + CCstats(cc_no) = stats(comp); + lower_range_overlap_comp = findLabels(lower_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); %Make to 1 for speed + upper_range_overlap_comp = findLabels(upper_range_bwimage(CC_scan_img.PixelIdxList{comp}),2); + + if (lower_range_overlap_comp(1,2) ~= 0 || lower_range_overlap_comp(1,1) == 0 ) + if show_error + fprintf('\n Wrong Calculation in LOWER Range Check Image'); + figure('Name','ERROR:No Overlap Component !! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:Overlap Component !! The Lower Range Overlap Image'); + error_figure(:,:) = 0; + imshow(lower_range_check_img); + end + continue; + end + + if (upper_range_overlap_comp(1,2) ~= 0 || upper_range_overlap_comp(1,1) == 0 ) + if show_error + fprintf('\n Wrong Calculation in LOWER Range Check Image'); + figure('Name','ERROR:No Overlap Component!! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:No Overlap Component!! The Upper Range Overlap Image'); + error_figure(:,:) = 0; + imshow(upper_range_check_img); + end + continue; + end + + comp_num_of_pixels = numel(CC_scan_img.PixelIdxList{comp}); + + upper_range_comp_no_pixels = numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}); + lower_range_comp_no_pixels = numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}); + + if show_error && (comp_num_of_pixels > lower_range_comp_no_pixels) + fprintf('\nError in Finding Correct LOWER Range Component: Size of Overlap reduced '); + figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:Size Reduction!! The Lower Range Overlap Image'); + error_figure(:,:) = 0; + imshow(lower_range_check_img); + figure('Name','ERROR:Size Redution!! The Lower Range Overlap Component'); + error_figure(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) = 1; + imshow(error_figure); + error_figure(:,:) = 0; + continue; + end + + if show_error && (comp_num_of_pixels > upper_range_comp_no_pixels) + fprintf('\nError in Finding Correct UPPER Range Component: Size of Overlap reduced '); + figure('Name','ERROR: Size Reduction!! K-means Component being scanned'); + error_figure(CC_scan_img.PixelIdxList{comp}) = 1; + imshow(error_figure); + figure('Name','ERROR:Size Reduction!! The Upper Range Overlap Image'); + error_figure(:,:) = 0; + imshow(upper_range_check_img); + figure('Name','ERROR:Size Redution!! The Upper Range Overlap Component'); + error_figure(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) = 1; + imshow(error_figure); + error_figure(:,:) = 0; + continue; + end + + FeatureValues(1,1) = (abs(numel(lower_range_check_CC.PixelIdxList{lower_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); + FeatureValues(1,2) = (abs(numel(upper_range_check_CC.PixelIdxList{upper_range_overlap_comp(1,1)}) - numel(CC_scan_img.PixelIdxList{comp})))/numel(CC_scan_img.PixelIdxList{comp}); + FeatureValues(1,3) = (lower_check_stats(lower_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,4) = (upper_check_stats(upper_range_overlap_comp(1,1)).EulerNumber - stats(comp).EulerNumber)/100; + FeatureValues(1,5) = (abs(lower_check_stats(lower_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,6) = (abs(upper_check_stats(upper_range_overlap_comp(1,1)).Solidity - stats(comp).Solidity))/ stats(comp).Solidity; + FeatureValues(1,7) = numel(CC_scan_img.PixelIdxList{comp})/(row*col); + FeatureValues(1,8) = stats(comp).BoundingBox(4)/row; + FeatureValues(1,9) = stats(comp).BoundingBox(3)/col; + + palate(CC_scan_img.PixelIdxList{comp}) = 1; + + FeatureValues(1,10:15) = [ stats(comp).Solidity (stats(comp).EulerNumber)/100 stats(comp).Eccentricity stats(comp).Extent SWT(palate) eHOG(palate) ]; + palate(:,:) = 0; + + + Features(cc_no,:) = FeatureValues; + + + end + + if getFinalBinImages + FinalBinImages(:,:,img_no) = scan_img; + end + + end + + q_offset = q_offset+2*main_offset-1; + end + + + + +end \ No newline at end of file diff --git a/GetBoundingBoxes.m b/GetBoundingBoxes.m index 0ada409..ed47e54 100644 --- a/GetBoundingBoxes.m +++ b/GetBoundingBoxes.m @@ -1,205 +1,225 @@ function [BoundingBoxes] = GetBoundingBoxes(CCs,CCstats,Features,NeedToStabilize,hasParametersSupplied,Parameters) - BoundingBoxes = zeros(1,4); - - % NOTE: The Feature Values supplied need to be of already stable - % componenets if hasParametersSupplied is false. - - %% Features - - % 1. Lower Range Pixel Deviatiion ([0,1]) - % 2. Higher Range Pixel Deviation ([0,1]) - % 3. Lower Range change of Euler Number/100([0,1)) - % 4. Higher Range change of Euler Number/100([0,1)) - % 5. Lower Range Density Deviation ([0,1]) - % 6. Higher Range Density Deviation ([0,1]) - % 7. No. of Pixels [0,1) - % 8. Height (1,1) - % 9. Width (1,1) - % 10. Solidity [0,1] - % 11. Euler/100 [0,1) - - % 12 Eccentricity [0,1] - % 13. Extent [0,1] - % 14. SVT [0,1] - % 15. eHOG [0,1] - - %% PARAMETERS - - % 1. Max Lower Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1 ) - % 2. Max Higher Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1) - % 3. Max Euler Number( Value > 1 and Value < Infinite ) - % 4. Max Difference in Euler Number for Lower Range/100( 0 <= Value < 1 ) - % 5. Max Difference in Euler Number for Higher Range/100( 0 <= Value < 1 ) - % 6. Max Solidity ( Value > 0 and Value < 1) - % 7. Min Solidity ( Value > 0 and Value < 1) - % 8. Max Lower Range Density Deviation Allowed ( 0 < Value < 1 ) - % 9. Max Higher Range Density Deviation Allowed ( 0 < Value < 1 ) - % 10. Max No. of Pixels - % 11. Min No. of Pixels - % 12. Max Height - % 13. Min Height - % 14. Max Width - % 15. Min Width - - - % 16. Baseline Deviation by average height for aligned ( 0 < Value < 1 ) - % 17. Spacing Deviation by average height for aligned (0 < value < 1 ) - % 18. Height Difference by average height for aligned ( 0 < Value < 1 ) - % 19. Maximum negative starting point by average height for aligned (0 < Value < 1) - - - % 20. Max Average Solidity for aligned ( 0 < Value < 1) - % 21. Min Average Solidity for aligned ( 0 < Value < Max Solidity) - % 22. Max Average Euler Number for aligned ( 0 < Value < 1) - % 23. Min Average Euler Number for aligned ( 0 < Value < Max Euler) - % 24. Max Average Eccentricity for Aligned ( 0 < Value < 1) - % 25. Min Average Eccentricity for Aligned ( 0 < Value < Max Eccentricity) - % 26. Max Average Extent for aligned ( 0 < Value < 1) - % 27. Min Average Extent for aligned ( 0 < Value < Max Extent) - % 28. Max Average SVT for aligned ( 0 < Value < 1) - % 29. Max Average eHOG for aligned ( 0 < Value < 1) - - % 20. Max Solidity [0,1] - % 31. Min Solidity [0,Max Solidity] - % 32. Max Euler Number [0,1) - % 33. Min Euler Number [0,Max Euler) - % 34. Max Eccentricity [0,1] - % 35. Min Eccentricity [0,Max Eccentricity] - % 36. Max Extent [0,1] - % 37. Min Extent [0, Max Extent) - % 38. Max SVT [0,1] - % 39. Max eHOG [0,1] +BoundingBoxes = zeros(1,4); +Features = abs(Features); +global CheckTextGroup; + + %Parameters +% NOTE: The Feature Values supplied need to be of already stable +% componenets if hasParametersSupplied is false. + +%% Features + +% 1. Lower Range Pixel Deviatiion ([0,1]) +% 2. Higher Range Pixel Deviation ([0,1]) +% 3. Lower Range change of Euler Number/100([0,1)) +% 4. Higher Range change of Euler Number/100([0,1)) +% 5. Lower Range Density Deviation ([0,1]) +% 6. Higher Range Density Deviation ([0,1]) +% 7. No. of Pixels [0,1) +% 8. Height [0,1) +% 9. Width [0,1) +% 10. Solidity [0,1] +% 11. Euler/100 [0,1) + +% 12 Eccentricity [0,1] +% 13. Extent [0,1] +% 14. SVT [0,1] +% 15. eHOG [0,1] + +%% PARAMETERS + +% 1. Max Lower Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1 ) +% 2. Max Higher Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1) +% 3. Max Euler Number/100( Value > 1 and Value < Infinite ) +% 4. Max Difference in Euler Number for Lower Range/100( 0 <= Value < 1 ) +% 5. Max Difference in Euler Number for Higher Range/100( 0 <= Value < 1 ) +% 6. Max Solidity ( Value > 0 and Value < 1) +% 7. Min Solidity ( Value > 0 and Value < 1) +% 8. Max Lower Range Density Deviation Allowed ( 0 < Value < 1 ) +% 9. Max Higher Range Density Deviation Allowed ( 0 < Value < 1 ) +% 10. Max No. of Pixels Ratio +% 11. Min No. of Pixels Ratio +% 12. Max Height Ratio +% 13. Min Height Ratio +% 14. Max Width Ratio +% 15. Min Width Ratio + + +% 16. Baseline Deviation by average height for aligned ( 0 < Value < 1 ) +% 17. Spacing Deviation by average height for aligned (0 < value < 1 ) +% 18. Height Difference by average height for aligned ( 0 < Value < 1 ) +% 19. Maximum negative starting point by average height for aligned (0 < Value < 1) + + +% 20. Max Average Solidity for aligned ( 0 < Value < 1) +% 21. Min Average Solidity for aligned ( 0 < Value < Max Solidity) +% 22. Max Average Euler Number for aligned ( 0 < Value < 1) +% 23. Min Average Euler Number for aligned ( 0 < Value < Max Euler) +% 24. Max Average Eccentricity for Aligned ( 0 < Value < 1) +% 25. Min Average Eccentricity for Aligned ( 0 < Value < Max Eccentricity) +% 26. Max Average Extent for aligned ( 0 < Value < 1) +% 27. Min Average Extent for aligned ( 0 < Value < Max Extent) +% 28. Max Average SVT for aligned ( 0 < Value < 1) +% 29. Max Average eHOG for aligned ( 0 < Value < 1) + +% 30. Max Solidity [0,1] +% 31. Min Solidity [0,Max Solidity] +% 32. Max Euler Number [0,1) +% 33. Min Euler Number [0,Max Euler) +% 34. Max Eccentricity [0,1] +% 35. Min Eccentricity [0,Max Eccentricity] +% 36. Max Extent [0,1] +% 37. Min Extent [0, Max Extent) +% 38. Max SVT [0,1] +% 39. Max eHOG [0,1] + +% data = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.25 0.5 0.25 0.5 +% 0.25 0.25 0.25 0.25 0.25 0.8 0.1 0.06 0 0.98 0 0.9 0 0.25 0.3 +% 0.65 0.1 0.06 0 0.99 0 0.9 0.2 0.25 0.3] +%% Parameter Initialization + +if hasParametersSupplied + GroupingParams = Parameters(1,16:19); % PARAMETER ARRAY DEPENDENT + AlignedGroupParams_MAX = [Parameters(1,20:2:26) Parameters(1,28:29)]; + AlignedGroupParams_MIN = [Parameters(1,21:2:27) 0 0]; + AlignedGroupParams_MAX = AlignedGroupParams_MAX + AlignedGroupParams_MIN; + NonAlignedGroupParams_MAX = [Parameters(1,30:2:36) Parameters(1,38:39)]; + NonAlignedGroupParams_MIN = [Parameters(1,31:2:37) 0 0]; + NonAlignedGroupParams_MAX = NonAlignedGroupParams_MAX + NonAlignedGroupParams_MIN; +else % DEFAULT VALUES + GroupingParams = [ 0.25 0.25 0.25 0.25 ]; + AlignedGroupParams_MAX = [ 0.8 0.06 0.98 0.9 0.25 0.3]; + AlignedGroupParams_MIN = [0.1 0 0 0.2 0 0]; + NonAlignedGroupParams_MAX = [0.65 0.06 0.99 0.9 0.25 0.3]; + NonAlignedGroupParams_MIN = [ 0.1 0 0 0.2 0 0]; +end + + +%% CODE +start = 1; +numBBs = 0; +component_class = zeros(size(Features,1),1); % Vector Denoting Class +% 0 - Undecided +% 1 - Non Text +% 2 - Text +% 3 - Stable Component + +for i = 1:size(CCs,2) + if CCs(1,i) == 0 + continue; + end + ends = CCs(1,i); - %% Parameter Initialization - - if hasParametersSupplied - GroupingParams = Parameters(1,16:19); % PARAMETER ARRAY DEPENDENT - AlignedGroupParams_MAX = [Parameters(1,20:2:26) Parameters(1,28:29)]; - AlignedGroupParams_MIN = [Parameters(1,21:2:27) 0 0]; - NonAlignedGroupParams_MAX = [Parameters(1,30:2:36) Parameters(1,38:39)]; - NonAlignedGroupParams_MIN = [Parameters(1,31:2:37) 0 0]; - else % DEFAULT VALUES - GroupingParams = [ 0.25 0.25 0.25 0.25 ]; - AlignedGroupParams_MAX = [ 0.8 0.06 0.98 0.9 0.25 0.3]; - AlignedGroupParams_MIN = [0.1 0 0 0.2 0 0]; - NonAlignedGroupParams_MAX = [0.65 0.06 0.99 0.9 0.25 0.3]; - NonAlignedGroupParams_MIN = [ 0.1 0 0 0.2 0 0]; - end - + - %% CODE - start = 1; - numBBs = 0; - component_class = zeros(size(Features,1),1); % Vector Denoting Class - % 0 - Undecided - % 1 - Non Text - % 2 - Text - % 3 - Stable Component - - for i = 1:numel(CCs) - ends = start + CCs(i).NumObjects -1; - - for comp = start:ends - if component_class(comp,1) == 1 || component_class(comp,1) == 2 %If already classified,skip over - continue - end - - if NeedToStabilize && component_class(comp,1) == 0 - if hasParametersSupplied - [isStable] = PredictStabilityFromParameters(Features(1,1:11),Parameters(1,1:15)); - else - error('Need to supply Parameters if Unstable components supplied'); - end - - if ~isStable - component_class(comp,1) = 1; - continue - else - component_class(comp,1) = 3; - end - end % Stabilization done - - - stats = CCstats(comp); - - avg_height = stats.BoundingBox(4); - avg_baseline = stats.BoundingBox(2) + stats.BoundingBox(4); - max_y = stats.BoundingBox(1) + stats.BoundingBox(3); - max_x = stats.BoundingBox(2) + stats.BoundingBox(4); - aligned_comps = zeros(1,20); % An Array of aligned comp labels, BUFFER OF UPTO 20 ALIGNED COMPONENTS - aligned_comps(1,:) = comp; - aligns = 1; - sum_features = Features(comp,:); - - BB = stats.BoundingBox; - - for scan_comp = (comp+1):ends - - if NeedToStabilize && component_class(scan_comp,1) == 0 - if hasParametersSupplied - [isStable] = PredictStabilityFromParameters(Features(1,1:11),Parameters(1,1:14)); - else - error('Need to supply Parameters if Unstable components supplied'); - end - - if ~isStable - component_class(scan_comp,1) = 1; - continue - else - component_class(scan_comp,1) = 3; - end - end %Stabilization done - - - scan_stats = CCstats(scan_comp); - spacing_dev = abs(scan_stats.BoundingBox(1) - max_y)/avg_height; - if spacing_dev > 1.25*GroupingParams(1,2) % CRITICAL ASSUME: Once components reach a certain distance horizontally,it keeps on increasing - break; - end - baseline_dev = abs(scan_stats.BoundingBox(2) + scan_stats.BoundingBox(4) - avg_baseline)/avg_height; - height_dev = abs(scan_stats.BoundingBox(4) - avg_height)/avg_height; - startPoint_diff = (max_y - scan_stats.BoundingBox(1))/avg_height; - - C_Arr = [baseline_dev spacing_dev height_dev startPoint_diff]; - isAligned = C_Arr <= GroupingParams; - - if isAligned == 1 - - avg_height = (avg_height*aligns + scan_stats.BoundingBox(4))/(aligns+1); - avg_baseline = (avg_baseline*aligns + (scan_stats.BoundingBox(2) + scan_stats.BoundingBox(4)))/(aligns+1); - max_y = max(max_y,scan_stats.BoundingBox(1)+scan_stats.BoundingBox(3)); - max_x = max(max_x,scan_stats.BoundingBox(2)+scan_stats.BoundingBox(4)); - aligns = aligns + 1; - aligned_comps(1,aligns) = scan_comp; - sum_features = sum_features + Features(scan_comp,:); - - BB(1,1) = min(BB(1,1),scan_stats.BoundingBox(1)); %%Increasing the Bounding Box - BB(1,3) = max_y - BB(1,1); - BB(1,2) = min(BB(1,2),scan_stats.BoundingBox(2)); - BB(1,4) = max_x - BB(1,2); - end - - end - - C_Arr = sum_features./aligns; - - if aligns > 1 - isTextGroup = (C_Arr <= AlignedGroupParams_MAX) & (C_Arr >= AlignedGroupParams_MIN); - else - isTextGroup = (C_Arr <= NonAlignedGroupParams_MAX ) & ( C_Arr >= NonAlignedGroupParams_MIN ); - end - - if isTextGroup == 1 - numBBs = numBBs + 1; - BoundingBoxes(numBBs,:) = BB; - component_class(aligned_comps,1) = 2; - else - component_class(aligned_comps,1) = 1; - end - - end - start = ends + 1; - end - -end \ No newline at end of file + for comp = start:ends + if component_class(comp,1) == 1 || component_class(comp,1) == 2 %If already classified,skip over + continue + end + + if NeedToStabilize && component_class(comp,1) == 0 + if hasParametersSupplied + [isStable] = PredictStabilityFromParameters(Features(comp,1:11),Parameters(1,1:15)); + else + error('Need to supply Parameters if Unstable components supplied'); + end + + if ~isStable + component_class(comp,1) = 1; + continue + else + component_class(comp,1) = 3; + end + end % Stabilization done + + + stats = CCstats(comp,:); + + avg_height = stats(1,4); + avg_baseline = stats(1,2) + stats(1,4); + max_y = stats(1,1) + stats(1,3); + max_x = stats(1,2) + stats(1,4); + aligned_comps = zeros(1,20); % An Array of aligned comp labels, BUFFER OF UPTO 20 ALIGNED COMPONENTS + aligned_comps(1,:) = comp; + aligns = 1; + sum_features = Features(comp,10:15); + + BB = stats(:,:); + + for scan_comp = (comp+1):ends + + if component_class(scan_comp,1) ~= 0 && component_class(scan_comp,1) ~= 3 + continue; + end + + if NeedToStabilize && component_class(scan_comp,1) == 0 + if hasParametersSupplied + [isStable] = PredictStabilityFromParameters(Features(scan_comp,1:11),Parameters(1,1:15)); + else + error('Need to supply Parameters if Unstable components supplied'); + end + + if ~isStable + component_class(scan_comp,1) = 1; + continue + else + component_class(scan_comp,1) = 3; + end + end %Stabilization done + + + scan_stats = CCstats(scan_comp,:); + spacing_dev = abs(scan_stats(1,1) - max_y)/avg_height; +% if spacing_dev > 1.25*GroupingParams(1,2) % CRITICAL ASSUME: Once components reach a certain distance horizontally,it keeps on increasing +% break; +% end + baseline_dev = abs(scan_stats(1,2) + scan_stats(1,4) - avg_baseline)/avg_height; + height_dev = abs(scan_stats(1,4) - avg_height)/avg_height; + startPoint_diff = (max_y - scan_stats(1,1))/avg_height; + + C_Arr = [baseline_dev spacing_dev height_dev startPoint_diff]; + isAligned = C_Arr <= GroupingParams; + + if isAligned == 1 + + avg_height = (avg_height*aligns + scan_stats(1,4))/(aligns+1); + avg_baseline = (avg_baseline*aligns + (scan_stats(1,2) + scan_stats(1,4)))/(aligns+1); + max_y = max(max_y,scan_stats(1,1)+scan_stats(1,3)); + max_x = max(max_x,scan_stats(1,2)+scan_stats(1,4)); + aligns = aligns + 1; + aligned_comps(1,aligns) = scan_comp; + sum_features = sum_features + Features(scan_comp,10:15); + + BB(1,1) = min(BB(1,1),scan_stats(1,1)); %%Increasing the Bounding Box + BB(1,3) = max_y - BB(1,1); + BB(1,2) = min(BB(1,2),scan_stats(1,2)); + BB(1,4) = max_x - BB(1,2); + end + + end +% + C_Arr = sum_features./aligns; + + if aligns > 1 + isTextGroup = (C_Arr <= AlignedGroupParams_MAX) & (C_Arr >= AlignedGroupParams_MIN); + + else + isTextGroup = (C_Arr <= NonAlignedGroupParams_MAX ) & ( C_Arr >= NonAlignedGroupParams_MIN ); + end + + if isTextGroup == 1 + numBBs = numBBs + 1; + BoundingBoxes(numBBs,:) = BB; + component_class(aligned_comps,1) = 2; + else + component_class(aligned_comps,1) = 1; + end + CheckTextGroup = CheckTextGroup + isTextGroup; + end + start = ends + 1; +end + + CountBoxes = size(BoundingBoxes,1); +end + diff --git a/MapBB.m b/MapBB.m new file mode 100644 index 0000000..c975c4c --- /dev/null +++ b/MapBB.m @@ -0,0 +1,238 @@ +function MapBB(CCs,CCstats,Features,NeedToStabilize,hasParametersSupplied,Parameters) +global ShowOutput CheckTextGroup CountStables; + +CountStables = 0; + +[row,col,~] = size(ShowOutput); +BoundingBoxes = zeros(1,4); +Features = abs(Features); +% NOTE: The Feature Values supplied need to be of already stable +% componenets if hasParametersSupplied is false. + +%% Features + +% 1. Lower Range Pixel Deviatiion ([0,1]) +% 2. Higher Range Pixel Deviation ([0,1]) +% 3. Lower Range change of Euler Number/100([0,1)) +% 4. Higher Range change of Euler Number/100([0,1)) +% 5. Lower Range Density Deviation ([0,1]) +% 6. Higher Range Density Deviation ([0,1]) +% 7. No. of Pixels [0,1) +% 8. Height [0,1) +% 9. Width [0,1) +% 10. Solidity [0,1] +% 11. Euler/100 [0,1) + +% 12 Eccentricity [0,1] +% 13. Extent [0,1] +% 14. SVT [0,1] +% 15. eHOG [0,1] + +%% PARAMETERS + +% 1. Max Lower Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1 ) +% 2. Max Higher Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1) +% 3. Max Euler Number/100( Value > 1 and Value < Infinite ) +% 4. Max Difference in Euler Number for Lower Range/100( 0 <= Value < 1 ) +% 5. Max Difference in Euler Number for Higher Range/100( 0 <= Value < 1 ) +% 6. Max Solidity ( Value > 0 and Value < 1) +% 7. Min Solidity ( Value > 0 and Value < 1) +% 8. Max Lower Range Density Deviation Allowed ( 0 < Value < 1 ) +% 9. Max Higher Range Density Deviation Allowed ( 0 < Value < 1 ) +% 10. Max No. of Pixels Ratio +% 11. Min No. of Pixels Ratio +% 12. Max Height Ratio +% 13. Min Height Ratio +% 14. Max Width Ratio +% 15. Min Width Ratio + + +% 16. Baseline Deviation by average height for aligned ( 0 < Value < 1 ) +% 17. Spacing Deviation by average height for aligned (0 < value < 1 ) +% 18. Height Difference by average height for aligned ( 0 < Value < 1 ) +% 19. Maximum negative starting point by average height for aligned (0 < Value < 1) + + +% 20. Max Average Solidity for aligned ( 0 < Value < 1) +% 21. Min Average Solidity for aligned ( 0 < Value < Max Solidity) +% 22. Max Average Euler Number for aligned ( 0 < Value < 1) +% 23. Min Average Euler Number for aligned ( 0 < Value < Max Euler) +% 24. Max Average Eccentricity for Aligned ( 0 < Value < 1) +% 25. Min Average Eccentricity for Aligned ( 0 < Value < Max Eccentricity) +% 26. Max Average Extent for aligned ( 0 < Value < 1) +% 27. Min Average Extent for aligned ( 0 < Value < Max Extent) +% 28. Max Average SVT for aligned ( 0 < Value < 1) +% 29. Max Average eHOG for aligned ( 0 < Value < 1) + +% 30. Max Solidity [0,1] +% 31. Min Solidity [0,Max Solidity] +% 32. Max Euler Number [0,1) +% 33. Min Euler Number [0,Max Euler) +% 34. Max Eccentricity [0,1] +% 35. Min Eccentricity [0,Max Eccentricity] +% 36. Max Extent [0,1] +% 37. Min Extent [0, Max Extent) +% 38. Max SVT [0,1] +% 39. Max eHOG [0,1] + +% data = [0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.25 0.5 0.25 0.5 +% 0.25 0.25 0.25 0.25 0.25 0.8 0.1 0.06 0 0.98 0 0.9 0 0.25 0.3 +% 0.65 0.1 0.06 0 0.99 0 0.9 0.2 0.25 0.3] +%% Parameter Initialization + +if hasParametersSupplied + GroupingParams = Parameters(1,16:19); % PARAMETER ARRAY DEPENDENT + AlignedGroupParams_MAX = [Parameters(1,20:2:26) Parameters(1,28:29)]; + AlignedGroupParams_MIN = [Parameters(1,21:2:27) 0 0]; + AlignedGroupParams_MAX = AlignedGroupParams_MAX + AlignedGroupParams_MIN; + NonAlignedGroupParams_MAX = [Parameters(1,30:2:36) Parameters(1,38:39)]; + NonAlignedGroupParams_MIN = [Parameters(1,31:2:37) 0 0]; + NonAlignedGroupParams_MAX = NonAlignedGroupParams_MAX + NonAlignedGroupParams_MIN; +else % DEFAULT VALUES + GroupingParams = [ 0.25 0.25 0.25 0.25 ]; + AlignedGroupParams_MAX = [ 0.8 0.06 0.98 0.9 0.25 0.3]; + AlignedGroupParams_MIN = [0.1 0 0 0.2 0 0]; + NonAlignedGroupParams_MAX = [0.65 0.06 0.99 0.9 0.25 0.3]; + NonAlignedGroupParams_MIN = [ 0.1 0 0 0.2 0 0]; +end + + +%% CODE +start = 1; +numBBs = 0; +component_class = zeros(size(Features,1),1); % Vector Denoting Class +% 0 - Undecided +% 1 - Non Text +% 2 - Text +% 3 - Stable Component + +for i = 1:size(CCs,2) + if CCs(1,i) == 0 + continue; + end + ends = CCs(1,i); + + ACC = bwconncomp(ShowOutput(:,:,(2*(i-1)+1))); + + for comp = start:ends + if component_class(comp,1) == 1 || component_class(comp,1) == 2 %If already classified,skip over + continue + end + + if NeedToStabilize && component_class(comp,1) == 0 + if hasParametersSupplied + [isStable] = PredictStabilityFromParameters(Features(comp,1:11),Parameters(1,1:15)); + else + error('Need to supply Parameters if Unstable components supplied'); + end + + if ~isStable + component_class(comp,1) = 1; + continue + else + component_class(comp,1) = 3; + end + end % Stabilization done + CountStables = CountStables + 1; + + stats = CCstats(comp,:); + + avg_height = stats(1,4); + avg_baseline = stats(1,2) + stats(1,4); + max_y = stats(1,1) + stats(1,3); + max_x = stats(1,2) + stats(1,4); + aligned_comps = zeros(1,20); % An Array of aligned comp labels, BUFFER OF UPTO 20 ALIGNED COMPONENTS + aligned_comps(1,:) = comp; + aligns = 1; + sum_features = Features(comp,10:15); + + BB = stats(:,:); + + for scan_comp = (comp+1):ends + + if component_class(scan_comp,1) ~= 0 && component_class(scan_comp,1) ~= 3 + continue; + end + + if NeedToStabilize && component_class(scan_comp,1) == 0 + if hasParametersSupplied + [isStable] = PredictStabilityFromParameters(Features(scan_comp,1:11),Parameters(1,1:15)); + else + error('Need to supply Parameters if Unstable components supplied'); + end + + if ~isStable + component_class(scan_comp,1) = 1; + continue + else + component_class(scan_comp,1) = 3; + end + end %Stabilization done + + + scan_stats = CCstats(scan_comp,:); + spacing_dev = abs(scan_stats(1,1) - max_y)/avg_height; +% if spacing_dev > 1.25*GroupingParams(1,2) % CRITICAL ASSUME: Once components reach a certain distance horizontally,it keeps on increasing +% break; +% end + baseline_dev = abs(scan_stats(1,2) + scan_stats(1,4) - avg_baseline)/avg_height; + height_dev = abs(scan_stats(1,4) - avg_height)/avg_height; + startPoint_diff = (max_y - scan_stats(1,1))/avg_height; + + C_Arr = [baseline_dev spacing_dev height_dev startPoint_diff]; + isAligned = C_Arr <= GroupingParams; + + if isAligned == 1 + + avg_height = (avg_height*aligns + scan_stats(1,4))/(aligns+1); + avg_baseline = (avg_baseline*aligns + (scan_stats(1,2) + scan_stats(1,4)))/(aligns+1); + max_y = max(max_y,scan_stats(1,1)+scan_stats(1,3)); + max_x = max(max_x,scan_stats(1,2)+scan_stats(1,4)); + aligns = aligns + 1; + aligned_comps(1,aligns) = scan_comp; + sum_features = sum_features + Features(scan_comp,10:15); + + BB(1,1) = min(BB(1,1),scan_stats(1,1)); %%Increasing the Bounding Box + BB(1,3) = max_y - BB(1,1); + BB(1,2) = min(BB(1,2),scan_stats(1,2)); + BB(1,4) = max_x - BB(1,2); + end + + end +% + C_Arr = sum_features./aligns; + + if aligns > 1 + isTextGroup = (C_Arr <= AlignedGroupParams_MAX) & (C_Arr >= AlignedGroupParams_MIN); + + else + isTextGroup = (C_Arr <= NonAlignedGroupParams_MAX ) & ( C_Arr >= NonAlignedGroupParams_MIN ); + end + + if isTextGroup == 1 + numBBs = numBBs + 1; + BoundingBoxes(numBBs,:) = BB; + BB = round(BB); + BB = max(1,BB); + + img = ShowOutput(:,:,2*i); +% for al = 1:aligns +% img(ACC.PixelIdxList{aligned_comps(1,al)}) = 1; +% end + img(BB(2),BB(1):min(col,BB(1)+BB(3))) = 1; + img(min(row,BB(2)+BB(4)),BB(1):min(col,BB(1)+BB(3))) = 1; + img(BB(2):min(row,BB(2)+BB(4)),BB(1)) = 1; + img(BB(2):min(row,BB(2)+BB(4)),min(col,BB(1)+BB(3))) = 1; + ShowOutput(:,:,2*i) = img; + + component_class(aligned_comps,1) = 2; + else + component_class(aligned_comps,1) = 1; + end + CheckTextGroup = CheckTextGroup + isTextGroup; + end + start = ends + 1; +end + +end + diff --git a/NumFilesExtracted.mat b/NumFilesExtracted.mat new file mode 100644 index 0000000..836fe9e Binary files /dev/null and b/NumFilesExtracted.mat differ diff --git a/PredictStabilityFromParameters.m b/PredictStabilityFromParameters.m index 52a39a4..560a7fa 100644 --- a/PredictStabilityFromParameters.m +++ b/PredictStabilityFromParameters.m @@ -1,5 +1,6 @@ function [isStable] = PredictStabilityFromParameters(Features,Parameters) - + global CheckisStable + %% PARAMETERS % 1. Max Lower Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1 ) % 2. Max Higher Range No. of Pixels Deviation Allowed ( Value > 0 and Value < 1) @@ -32,10 +33,12 @@ % 11. Euler [0,1) %% CODE - MAX_Params = Parameters(1,[1 2 4 5 8 9 10 12 14 6 3]); + MIN_Params = [0 0 0 0 0 0 Parameters(1,[11 13 15 7]) 0]; - - IsStable = (Features <= MAX_Params) & (Features>= MIN_Params); + MAX_Params = Parameters(1,[1 2 4 5 8 9 10 12 14 6 3]); +% Features(1,[3,4,11]) = 0; + IsStable = (Features <= MAX_Params) & (Features>= MIN_Params); + CheckisStable = CheckisStable + IsStable; if IsStable == 1 isStable = true; else diff --git a/ReduceToMainCCs.m b/ReduceToMainCCs.m index 40a8bef..8a632b9 100644 --- a/ReduceToMainCCs.m +++ b/ReduceToMainCCs.m @@ -1,29 +1,28 @@ -function [output_images] = ReduceToMainCCs(rgb_BinImages) - - min_size_threshold = 10; - min_vol_threshold = 50; - pixel_percent_threshold = 0.004; +function [images] = ReduceToMainCCs(images) + + +[row,col,numImages] = size(images); + +parfor img_no = 1:numImages + img = images(:,:,img_no); + CCimg = bwconncomp(img); - [row,col,numImages] = size(rgb_BinImages); - output_images = false(size(rgb_BinImages)); - temp_img = false(row,col); - for img_no = 1:numImages - img = rgb_BinImages(:,:,img_no); - CCimg = bwconncomp(img); - - temp_img(:,:) = 0; - - stat = regionprops(CCimg,'BoundingBox'); - - for comp_no = 1:CCimg.NumObjects - - if max(stat(comp_no).BoundingBox(3),stat(comp_no).BoundingBox(4)) > min_size_threshold && numel(CCimg.PixelIdxList{comp_no}) > min_vol_threshold && numel(CCimg.PixelIdxList{comp_no}) > pixel_percent_threshold*(row*col) - temp_img(CCimg.PixelIdxList{comp_no}) = 1; - end - + temp_img = zeros(row,col); + + + stat = regionprops(CCimg,'BoundingBox'); + + for comp_no = 1:CCimg.NumObjects + width = stat(comp_no).BoundingBox(3); + height = stat(comp_no).BoundingBox(4); + pixel_count = numel(CCimg.PixelIdxList{comp_no}); + if width > 0.04*min(row,col) && height > 0.04*min(row,col) && pixel_count > 0.000064*(row*col) + temp_img(CCimg.PixelIdxList{comp_no}) = 1; end - output_images(:,:,img_no) = temp_img; end + + images(:,:,img_no) = temp_img; +end end \ No newline at end of file diff --git a/ShowOutputImages2.mat b/ShowOutputImages2.mat new file mode 100644 index 0000000..1877445 Binary files /dev/null and b/ShowOutputImages2.mat differ diff --git a/SmallTest/i (8).jpg b/SmallTest/i (8).jpg new file mode 100644 index 0000000..fa5661c Binary files /dev/null and b/SmallTest/i (8).jpg differ diff --git a/SmallTest/i (9).jpg b/SmallTest/i (9).jpg new file mode 100644 index 0000000..4bfa814 Binary files /dev/null and b/SmallTest/i (9).jpg differ diff --git a/chromosomeRank.m b/chromosomeRank.m index eec16e5..23ea36a 100644 --- a/chromosomeRank.m +++ b/chromosomeRank.m @@ -1,15 +1,15 @@ -function [population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,flag,dflag) +function [population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,scale,flag,dflag) %1st flag if 1 then chromosomes are to be ranked %2nd dflag if 1 then display the population rng('shuffle'); [r,~]=size(population); if flag==1 for i=1:r - [rank(i)]=classify(population(i,:)); + [rank(i)]=classify(population(i,:)./scale); %rank(i)=rand(1); end end - [~,temp]=sort(rank); + [~,temp]=sort(rank, 'descend'); population=population(temp,:); rank=rank(temp); pbest=pbest(temp); diff --git a/datacreate.m b/datacreate.m index 3051156..828aecb 100644 --- a/datacreate.m +++ b/datacreate.m @@ -1,13 +1,36 @@ %creates a feature list & value of acuuracy of list out of 1(0-1) -function [data] = datacreate(n,dimension,lb,ub) - %n is the number of chromosomes we are working on - - rng('shuffle'); - data=zeros(n,dimension); - for i=1:n - temp=rand(1,dimension); - temp=temp*(ub-lb)+lb; - data(i,:)=temp; - end - clear max min count; +function [data] = datacreate(n,dimension,scale,lb,ub) +%n is the number of chromosomes we are working on + +rng('shuffle'); +data=zeros(n,dimension); +scaling_matrix = zeros(n,dimension,2); +ub = ub/scale; +for i=1:n + temp=rand(1,dimension); + scaling_matrix(i,:,:) = rand(1,dimension,2); +% temp=temp*(ub-lb)+lb; + data(i,:)=temp; +end +clear max min count; + + +%Finish this + +min_dev = 0.2; +max_k =4.8; + +%ideal = [0.25 0.25 1 0.25 0.25 1 0.1 0.25 0.25 0.15 0.0015 0.4 0.01 0.85 0.05 0.25 0.25 0.25 0.25 0.9 0.2 1 0 0.98 0 0.9 0.2 0.4 0.1 0.65 0.1 1 0 0.99 0 0.9 0.2 0.4 0.1 ]; +ideal = [0.25 0.25 1 0.25 0.25 1 0.1 0.25 0.25 0.15 0.00015 0.4 0.001 0.85 0.005 0.25 0.25 0.25 0.25 0.9 0.2 1 0 0.98 0 0.9 0.2 0.4 0.1 0.65 0.1 1 0 0.99 0 0.9 0.2 0.4 0.1 ]; +catch_all = [ 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 1]; +% % data(:,~ideal) = max(0.25,data(:,~ideal)); +%data(1,:) = catch_all; +for i=1:n + data(i,:) = (((max_k*data(i,:)).*scaling_matrix(i,:,1)) + (min_dev*scaling_matrix(i,:,2))).*ideal; +end +% % % data(1,:) = ideal; +% % data(:,16:19) = 0.25; +% data = max(data,lb); +% data = min(data,ub); +data=data*scale; end \ No newline at end of file diff --git a/gps.m b/gps.m index 01889a0..6519471 100644 --- a/gps.m +++ b/gps.m @@ -1,7 +1,7 @@ function [timeexc,gbest_rank]=gps() %% Indra's Code -global ExtractionDone; +global ExtractionDone ExtractionDone = false; %% @@ -10,17 +10,18 @@ %clc rng('shuffle'); functionNum = 1; +scale = 1; % these need variables to be changed for each function, refer to the tables in the paper -ubArray=ones(1,1); +ubArray=ones(1,1)*scale; lbArray=zeros(1,1); dimArray=39; -ub=ubArray(functionNum); -lb=lbArray(functionNum); +ub=3*ubArray; +lb=lbArray; dimension=dimArray(functionNum); % variables requiring change ends -n=5; %number of points being considered -iter=2; +n=50; %number of points being considered +iter=50; label=zeros(1,iter); valuesBest=zeros(1,iter); @@ -30,14 +31,14 @@ rank=vpa(zeros(1,n)); velocities=vpa(zeros(n,dimension)); %} -population=(datacreate(n,dimension,lb,ub)); +population = datacreate(n,dimension,scale,lb,ub); rank=(zeros(1,n)); velocities_gsa=(zeros(n,dimension)); velocities_pso=zeros(n,dimension); pbest=rank; pbest_particle=population; -[population,rank,velocities_gsa,velocities_pso,~,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,1,1); +[population,rank,velocities_gsa,velocities_pso,~,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,scale,1,1); gbest_rank=rank(1); gbest_particle=population(1,:); pbest=rank; @@ -46,7 +47,7 @@ for count=1:iter - %fprintf('Iteration - %d with gbest as - %f and current best %f change - %f\n',count,double(gbest_rank),double(rank(1)),change); + fprintf('Iteration - %d with gbest as - %f and current best %f\n',count,double(gbest_rank),double(rank(1))); valuesBest(1,count)=rank(1); valuesAvg(1,count)=sum(rank)/n; @@ -61,9 +62,9 @@ population=min(population,ub); population=max(population,lb); - [population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,1,0); + [population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,scale,1,1); - if (gbest_rank>=rank(1)) + if (gbest_rank<=rank(1)) gbest_rank=rank(1); gbest_particle=population(1,:); end @@ -82,10 +83,10 @@ else change=0; end - - %%{ + %disp(population); + %{ [population]=mutation(population,rank,count,iter,change,(ub-lb)); - [population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,1,0); + [population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle]=chromosomeRank(population,rank,velocities_gsa,velocities_pso,pbest,pbest_particle,scale,1,0); if (gbest_rank>=rank(1)) gbest_rank=rank(1); gbest_particle=population(1,:); diff --git a/i (101).jpg b/i (101).jpg new file mode 100644 index 0000000..a02233b Binary files /dev/null and b/i (101).jpg differ diff --git a/isReasonableSize.m b/isReasonableSize.m new file mode 100644 index 0000000..a4cdc2c --- /dev/null +++ b/isReasonableSize.m @@ -0,0 +1,11 @@ +function [truth] = isReasonableSize(width,height,pixel_count,row,col) + + %TODO + + if width > 0.04*min(row,col) && row > 0.04*min(row,col) && pixel_count > 0.000064*(row*col) && width < 0.25*col && height < 0.25*row && pixel_count < 0.0625*(row*col) + truth = true; + else + truth = false; + end + +end \ No newline at end of file diff --git a/untitled.jpg b/untitled.jpg new file mode 100644 index 0000000..d33e1db Binary files /dev/null and b/untitled.jpg differ diff --git a/viewWorking.m b/viewWorking.m new file mode 100644 index 0000000..f4a6a90 --- /dev/null +++ b/viewWorking.m @@ -0,0 +1,138 @@ +function viewWorking(isImageBinned,isImageCombined,image_no,ShowOutput_dir,file_ext) +global BinImages ShowOutput CheckisStable CheckTextGroup CountStables; + +CheckisStable = zeros(1,11); +CheckTextGroup = zeros(1,6); + +folderPath='with GT/'; +%folderPath = 'E:\ResearchFiles\DATA\test_input\'; % Path on Indra's Machine only +idir = dir(strcat(folderPath,'i (*).jpg')); +nfiles = 1; + +%load('ExtractedFileNames.mat') +% load('FCCs.mat') +% load('FCCs_indexes.mat') +% load('FCCstats.mat') +% load('FCCstats_indexes.mat') +% load('FFeatures.mat') +% load('FFeatures_indexes.mat') + +currentfilename = idir(image_no).name; + +imagePath=strcat(folderPath,currentfilename); +img = rgb2gray(imread(imagePath)); +image = img; + +fprintf(".....Binning...\n"); +MAX_DISTANCE = 255; +BinSizes = [32,50,62,77,95,118]; +if ~isImageBinned + [NumBinImages,~] = Binning(image,BinSizes); + fprintf(".....Saving Binned Images...\n"); + BinImagesSaved = BinImages; + save(strcat('BinImages',int2str(image_no),'.mat'),'BinImagesSaved'); +else + load(strcat('BinImages',int2str(image_no),'.mat')); + BinImages = BinImagesSaved; +end + + + +% BinMatrix = GetBinAllocations(BinSizes,MAX_DISTANCE,size(BinImages,3)); +% % if ~tFCCinitialized +% % tFCCs = zeros(nfiles,NumBinImages); +% % tFCCstats = zeros(nfiles,4); +% % tFCCinitialized = true; +% % end + +% StabilityMatrix = GetStabilityMatrix(BinSizes,BinMatrix,MAX_DISTANCE); +% fprintf("......Extracting Features........\n"); +% [row,col,~] = size(image); +% ShowOutput = false(row,col,2*size(BinImages,3)); +% [CCs,CCstats,Features,~] = GetAllFeatures(BinSizes,MAX_DISTANCE,StabilityMatrix,false); +% CCs + +% save('viewWorkingFeatures.mat','Features') +% save('viewWorkingCCstats.mat','CCstats') +% save('viewWorkingCCs.mat','CCs') + + +% + fprintf(".....Combining and removing components....\n"); +% +if ~isImageCombined + q_offset = 0; +for i = 1:numel(BinSizes) + main_offset = ceil(MAX_DISTANCE/BinSizes(i)); + for img_no = (q_offset+1):(q_offset+main_offset) + + if img_no ~= (q_offset+main_offset) + ShowOutput(:,:,(2*(img_no-1)+1)) = ReduceToMainCCs(logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no+main_offset)))); + else + ShowOutput(:,:,(2*(img_no-1)+1)) = ReduceToMainCCs(logical(BinImages(:,:,img_no))); + end + end + + for img_no = (q_offset+main_offset+1):(q_offset+2*main_offset-1) + ShowOutput(:,:,(2*(img_no-1)+1)) = ReduceToMainCCs(logical(BinImages(:,:,img_no)+BinImages(:,:,(img_no-main_offset+1)))); + end + + q_offset = q_offset + 2*main_offset -1; +end + ShowOutputSaved = ShowOutput; + save(strcat('ShowOutputImages',int2str(image_no),'.mat'),'ShowOutputSaved'); +else + load(strcat('ShowOutputImages',int2str(image_no),'.mat')); + ShowOutput = ShowOutputSaved; +end + +load('viewWorkingFeatures.mat','Features') +load('viewWorkingCCstats.mat','CCstats') +load('viewWorkingCCs.mat','CCs') + + Features(:,[3 4 11]) = Features(:,[3 4 11])./1000; + Features = abs(Features); +% img_no = image_no; +% hasParametersSupplied = true; +% +% startCC = FCCs_indexes(img_no,1); +% endCC = FCCs_indexes(img_no,2); +% +% startCCstats = FCCstats_indexes(img_no,1); +% endCCstats = FCCstats_indexes(img_no,2); +% +% startFeatures = FFeatures_indexes(img_no,1); +% endFeatures = FFeatures_indexes(img_no,2); +% +% CCs = FCCs(startCC:endCC,:); +% CCstats = FCCstats(startCCstats:endCCstats,:); +% Features = FFeatures(startFeatures:endFeatures,:); + +hasParametersSupplied = true; +parameters = [ 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 1]; +fprintf("......Get Bounding Boxes...\n"); +MapBB(CCs,CCstats,Features,true,hasParametersSupplied,parameters); + + +CheckisStable +CountStables +CheckTextGroup + + + +fprintf("......Printing......\n"); +NumImages = size(ShowOutput,3); +for j=(1:NumImages) + F_img = ShowOutput(:,:,j); + name = strrep('i (101).jpg',strcat('.',file_ext),''); + name3=strcat(name,'_'); + name3 = strcat(name3, int2str(j)); + saveFile3=strcat(ShowOutput_dir,name3,'.jpg'); + + imwrite(F_img,saveFile3,'jpg'); + +end + + +end + diff --git a/viewWorkingCCs.mat b/viewWorkingCCs.mat new file mode 100644 index 0000000..67fc657 Binary files /dev/null and b/viewWorkingCCs.mat differ diff --git a/viewWorkingCCstats.mat b/viewWorkingCCstats.mat new file mode 100644 index 0000000..4d82c7f Binary files /dev/null and b/viewWorkingCCstats.mat differ diff --git a/viewWorkingFeatures.mat b/viewWorkingFeatures.mat new file mode 100644 index 0000000..c75c2fb Binary files /dev/null and b/viewWorkingFeatures.mat differ