Clear Filters
Clear Filters

I want to find the distance of each cross from the center blue cross when the mat with grid lines is moving i.e. it will be a video.

1 view (last 30 days)
The aim of this project is to calculate distance of each cross from the center cross in terms of (x,y) pixel location. I have applied blob analysis method to firstly locate each cross but when im applying thresholding i am unable to remove all the other unwanted white area which is making it very difficult simultaneously i have also applied image segmentation but when the crosses are moving it is not detecting it both of my codes are presented below.
%Thresholdind Code
%% Load Sample Frames
v = imread('WIN_20240502_14_38_36_Pro.jpg');
subplot(1,3,1)
imshow(v);
% Convert RGB image to chosen color space
I = rgb2hsv(v);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.188;
channel1Max = 0.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.172;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.391;
channel3Max = 0.712;
% Create mask based on chosen histogram thresholds
sliderBW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Close mask with disk
% radius = 13;
% decomposition = 0;
% se = strel('disk', radius, decomposition);
% BW = imclose(BW, se);
% Close mask with disk
radius = 2;
decomposition = 0;
se = strel('disk', radius, decomposition);
BW = imdilate(BW, se);
figure;
subplot(2,1,1)
imshow(BW)
radius = 2;
d = 0;
se = strel('disk',radius,d);
BW= imclose(BW,se)
subplot(2,1,2)
imshow(BW)
%% Blob Analysis using Segmentation
hblob = vision.BlobAnalysis('MinimumBlobArea',200,'MaximumBlobArea',1000);
[objarea,objcent,bboxOut]= hblob(BW)
%% Annotate images
Image= insertShape(v,'rectangle',bboxOut,'linewidth',4);
figure
subplot(1,2,1)
imshow(Image)
%Image Segmentation Code
v = vision.VideoFileReader('WIN_20240502_15_10_30_Pro.mp4');
v.VideoOutputDataType = 'double';
hblob = vision.BlobAnalysis('MinimumBlobArea',200,'MaximumBlobArea',5000);
% Create video player object
vidPlayer = vision.DeployableVideoPlayer;
while ~isDone(v)
vidframe = step(v);
% Convert RGB image into L*a*b* color space.
X = rgb2lab(vidframe);
foregroundInd = [319346 319347 319348 319349 319350 319351 319352 320047 320063 320064 320783 321487 321502 322207 322220 322927 323647 323648 323658 324368 324377 325088 325097 325809 325817 326529 327250 327970 328691 329412 329417 330132 330136 330852 330856 331573 331574 331576 332294 332295 333014 333734 333735 334456 335174 335177 335893 336612 336618 337326 337327 337328 337329 337330 337331 337340 337341 338061 338782 339504 339505 339506 339507 516253 516973 517693 518414 518438 519135 519155 519156 519157 519855 519856 519875 520577 520592 520593 521298 521311 522018 522020 522030 522741 522749 523462 523463 523464 523468 524184 524187 524514 525625 525627 525954 525955 526293 526314 526345 526346 526347 527013 527032 527034 527067 527397 527417 527418 527420 527421 527733 527753 527784 527788 527789 528117 528118 528119 528136 528453 528454 528469 528471 528472 528504 528509 528839 528855 529174 529191 529223 529230 529560 529573 529574 529575 529894 529909 529910 529942 529952 530281 530282 530283 530292 530615 530629 530662 530672 531004 531008 531009 531010 531011 531336 531348 531382 531393 531725 531726 531727 531728 532056 532066 532067 532101 532114 532115 532445 532446 532447 532786 532818 532820 532836 533162 533163 533164 533165 533169 533502 533504 533538 533557 533880 533881 533890 533891 533892 534217 534221 534256 534257 534599 534613 534938 534940 534976 535317 535318 535334 535335 535659 535660 535689 535694 535695 536036 536055 536056 536378 536409 536410 536411 536413 536754 536755 536778 537097 537473 537474 537500 537501 537502 537816 537821 538192 538222 538223 538535 538912 538944 539254 539255 539264 539632 539664 539665 539973 539985 540352 540353 540693 540706 540707 541412 541428 542131 542148 542149 542851 542871 543591 544312 715310 716753 716754 717475 718195 718196 718917 718918 718938 719638 719639 719640 719657 720360 720362 720377 721084 721095 721096 721804 721815 722525 722526 722527 722535 723247 723254 723255 723968 723973 724689 724690 724692 724693 725411 725412 726129 726130 726131 726132 726847 726848 726853 726854 727567 727574 728284 728285 728286 728295 729004 729720 729721 729722 730439 731157 731876 733313 733315 734032 734750 734752 ];
backgroundInd = [14 82 857 862 1570 2307 2998 3750 4331 5770 5824 5866 5913 5915 5917 6490 6557 6568 8081 8649 8807 9531 9535 10089 10258 11701 12422 13142 14582 15848 18727 21780 24487 28087 31135 31687 36007 38334 38887 41047 45367 46972 49687 52567 55447 59767 61365 62647 64807 68407 70567 72157 73447 82807 88706 93607 99498 100807 104407 104535 109447 115330 116647 125287 127563 135367 135480 140519 145447 149154 160567 165708 170647 177224 183605 190902 198722 204579 217537 230495 243452 253529 263606 269365 272963 276561 280157 283033 285191 286630 287347 287349 288752 288753 288754 288756 288760 288765 288768 288771 288773 288777 288782 288784 289470 290186 290904 294501 296657 298095 298813 298814 299523 299525 299529 299530 299532 308903 308908 308912 308916 308917 308919 308928 308932 308939 308942 308948 308952 308954 308959 308963 308966 308968 308973 308981 308983 308988 308993 308994 308996 308998 309615 309616 309617 309620 309621 309641 309645 309723 309729 309735 309737 309741 309748 310334 310347 310353 310355 310472 310477 311053 311060 311772 311773 311774 311776 311921 311925 311926 312491 312648 312650 313211 313374 314098 314103 314333 314334 314336 314337 314338 314339 314340 314341 314342 314343 314345 314346 314348 314350 314351 314352 314355 314357 314358 314361 314363 314366 315052 315089 315092 315096 315099 315371 315552 315553 315560 315563 315565 315566 315569 315572 315576 315578 315581 315585 315588 315590 315592 315593 315596 315600 315602 315606 315609 315613 315617 315619 315623 315624 315626 315632 315633 315636 315638 315642 315646 315648 315771 315823 315824 316091 316372 316374 316376 316490 316548 316738 316743 316749 316752 316757 317099 317100 317102 317103 317104 317105 317209 318251 318548 318549 318551 318552 318553 318554 318555 318556 318557 318558 318559 318562 318564 318565 318566 318568 318569 318572 318574 318575 318578 318579 318648 318649 318713 318925 319300 319301 319367 319607 319609 319651 319691 320022 320025 320087 320157 320745 320746 320806 320882 320885 320890 320895 320898 321040 321466 321526 321754 321817 321851 322186 322187 322246 322343 322346 322351 322429 322437 322447 322453 322456 322465 322470 322965 323078 323264 323265 323291 323627 323685 323802 323860 324348 324404 324405 324529 324568 324707 324731 325069 325255 325276 325428 325789 325843 325844 325983 325988 326149 326562 326870 327231 327281 327282 327611 327953 327954 327955 327956 327998 327999 328000 328674 328676 328677 328678 328717 329390 329392 329437 329751 330109 330158 330471 330491 330826 330827 331211 331545 331546 331600 331601 332264 332651 332982 333352 333701 334421 334484 334792 335141 335206 335532 336232 336252 336581 336953 337301 337693 338021 338393 338741 339133 339461 340182 340248 340904 341273 341293 341624 342345 342409 343067 343433 344173 344507 345228 345614 345950 346009 346313 346671 347054 347753 348113 348169 348473 348834 349214 349554 349609 349913 350275 350329 350997 351049 351374 351717 351734 351735 351768 351769 352452 352456 352793 353158 353159 353171 353176 353208 353534 353880 353881 353882 353883 353884 353885 353886 353888 353890 353891 353897 353927 354618 354620 354621 354622 354623 354974 355343 355365 355366 355673 356065 356085 356786 356787 356788 356801 356802 356803 357508 357517 357518 357519 358229 358230 358232 358233 358234 358235 358236 359273 359993 360015 361455 362873 363593 364313 365033 365753 365776 366473 366496 367937 368633 370097 370817 371513 372233 372257 373673 374417 375113 375137 375833 375857 377273 377993 378017 380153 380177 381593 382338 383778 385940 386633 386660 387380 388073 388100 388820 389540 390260 390980 392421 394553 394581 396741 397461 398181 399593 400341 401033 402473 402501 403941 404633 406073 406101 406821 407513 407541 408261 408953 410393 411113 411861 413274 414021 415434 416154 417622 419034 419062 419754 422635 423383 424075 424103 424823 425543 426263 426956 427676 428423 429863 431276 433464 434876 434904 437036 437064 439197 440637 440665 441357 442077 442797 443517 444265 445678 445706 447118 449998 451467 453598 454348 455758 458668 460798 460828 463678 464429 465118 465869 467998 468030 468718 469438 469470 470158 470878 471598 471630 472318 475198 475230 477358 478798 479518 479551 480238 480958 481678 482431 483151 484558 484592 485312 486032 486752 487073 487074 487438 487472 487784 487785 487787 487793 488192 488509 488512 488513 488517 488912 489239 489242 489631 489953 489962 489964 490318 490349 490350 490685 491067 491068 491393 491758 491786 492113 492114 492130 492506 492835 493226 493555 493556 493577 493945 494302 494638 494664 494665 494998 495358 495384 495718 495748 496078 496104 496472 496798 497196 497199 497201 497518 497544 497879 498238 498264 498600 498643 499704 500041 500086 500398 500762 500806 501526 501864 502204 502558 502924 502966 503304 503603 504024 504042 504043 504044 504045 504046 504047 504048 504049 504050 504323 504365 504718 504744 504760 504761 504770 505085 505124 505465 505477 505478 505479 505490 505780 505807 505843 506185 506489 506499 506527 506878 506913 506930 507206 507207 507247 507281 507627 507629 507631 507632 507650 507918 507919 507920 507921 507922 507924 507940 507968 508000 508001 508318 508370 508632 508634 508636 508661 508664 508689 508719 509036 509037 509090 509344 509346 509348 509349 509384 509409 509756 509810 510063 510105 510129 510156 510474 510475 510530 510827 510849 510874 511187 511188 511190 511191 511192 511193 511502 511547 511570 511592 511902 511904 511905 511970 512220 512221 512268 512291 512310 512311 512618 512620 512621 512690 512989 513011 513029 513030 513331 513332 513333 513334 513335 513336 513337 513338 513409 513660 513708 513732 513747 514045 514046 514047 514048 514049 514050 514128 514380 514427 514428 514452 514467 514762 514763 514848 515146 515174 515175 515184 515477 515478 515479 515480 515481 515567 515819 515865 515895 515896 515897 515898 515901 516197 516286 516539 516584 516585 516917 517006 517259 517302 517637 517726 518021 518022 518699 518740 518778 519166 519460 520180 520606 520900 521326 521620 521959 522679 523019 523060 523486 523780 524458 524500 525940 526279 526660 526999 527380 527807 528100 528101 528821 529159 529248 529541 529968 530261 530599 530937 530981 531319 531409 531701 532039 532129 532421 532759 533141 533479 533569 533861 534199 534536 534581 535301 535640 536021 536450 536741 537081 537170 538181 538521 538610 539330 539573 539961 540050 540342 540770 541732 542210 542504 543562 543650 543891 543964 543965 543967 543968 543969 543971 544282 544370 544665 544666 544667 544683 545388 545414 545723 545794 545795 545796 545797 545799 545800 545810 546048 546109 546122 546135 546443 546511 546520 546530 546829 546841 547227 547229 547230 547250 547488 547549 547560 547576 547578 547946 547960 547961 548208 548269 548279 548604 548664 548665 548666 548690 548989 548997 548998 549021 549383 549401 549410 549648 550044 550101 550102 550130 550368 550429 550437 550462 550463 550764 550785 550786 551541 551562 551563 551564 551565 551566 551568 551569 551877 551904 552222 552224 552227 552528 552589 552924 552941 552947 552948 552977 552978 553248 553309 553345 553968 554029 554035 554364 554378 554388 554417 554708 554709 554712 554713 554716 554717 554718 554719 554720 554721 554723 554724 554725 554726 554727 554728 554729 554730 554731 554732 554734 554736 554737 554738 554739 554740 554741 554742 554744 554745 554746 554747 554748 554749 554787 555084 555097 555408 555422 555425 555461 555508 555804 555816 555829 555856 556128 556137 556182 556524 556535 556576 556848 556856 556914 556949 557245 557253 557254 557269 557295 557570 557572 557669 557965 557972 557989 557990 558014 558353 558389 558686 558687 558689 558690 558691 559073 559109 559431 559454 559827 560151 560512 560514 560516 560544 560871 561233 561239 561243 561250 561255 561262 561591 561953 562312 562334 563032 563752 564494 565913 565933 566634 567354 568075 568795 569515 570236 571693 572397 573117 573853 575277 575293 576717 578877 579597 581037 581053 582477 583917 583933 586075 587515 588253 588955 590394 591114 591133 592554 592573 593274 593993 595433 596153 597593 598312 598333 599032 599751 600493 601191 601910 602630 604069 604789 604813 606949 606973 608388 610573 611266 612733 614866 614893 615585 617745 617762 617765 617767 618482 619202 619213 619922 620625 621373 622096 622785 622802 623505 623536 625665 625693 626402 627133 627138 627167 627170 627172 627173 627174 627896 629282 629293 629985 630013 630042 630705 630733 630740 630760 632173 632216 632893 632936 633585 633635 634305 634333 635062 635762 635773 635782 635792 635816 637256 637974 638625 638653 638663 638694 639387 639413 640093 640133 640785 640825 641522 642224 642264 642292 643011 643664 643693 643704 644384 645133 645144 645824 645842 646544 646573 646610 648013 648050 648743 648744 649424 650144 650173 650864 651584 651602 651613 651624 651649 652304 653024 653744 654464 655249 655933 655943 656624 658064 658082 658128 658824 659533 660224 660285 662384 662413 662425 663842 664604 665264 665984 666001 667453 668144 668161 668181 668864 668881 668893 668922 670361 671742 671800 672462 672507 672519 673237 673238 673901 673933 673957 674653 674667 675340 675396 676779 676813 676836 677533 677556 678219 678973 678981 679716 680379 682539 682573 682587 682596 683293 683979 684036 686138 688298 688333 689738 689778 689796 691898 692653 692668 692676 694778 694816 696218 699096 699133 699876 700536 700590 702030 703414 704171 704506 704508 704509 704510 704511 704512 704541 704542 704543 704545 704546 704547 704548 704549 704550 704551 704854 704893 704910 705220 705221 705224 705225 705233 705234 705257 705258 705260 705938 705940 705955 705975 707070 707377 707395 707412 707733 707790 707796 708096 708116 708131 708155 708510 708815 708837 708849 709173 709534 709558 709559 709567 709568 709950 709956 710254 710280 710281 710282 710283 710284 710285 710320 710613 710648 710653 711042 712110 712414 712483 713134 713204 713493 713924 714253 714270 714574 714644 714965 715294 715363 715651 715710 716082 716734 716802 717091 717123 717133 717150 717156 717454 717521 717853 717871 718174 718240 718573 718591 718894 719293 719679 720013 720031 720335 720399 720733 721119 721454 722130 722176 722191 722195 722496 722559 722849 722898 723618 723622 723631 724719 725008 725064 725379 725726 725785 725793 726159 726446 726506 726821 726878 727163 727164 727165 727226 727881 727947 727948 727949 727950 727951 728266 728318 728594 728595 728597 728599 728601 728672 728986 729038 729311 729313 729484 729488 729495 729500 729503 729507 729512 729518 729522 729528 729532 729699 729701 729702 729704 729706 729758 729826 729829 729831 729833 729835 729838 729839 729840 729841 729842 729844 729846 729848 729850 729853 729857 729859 729864 729866 729872 729875 729878 729882 729886 729964 729965 729967 729969 729971 729973 729976 729978 729981 729983 729985 729988 729993 730029 730112 730255 730419 730478 730543 730613 730618 730680 730681 730682 730717 730742 730744 730746 730748 730918 730980 730984 731198 731221 731223 731226 731227 731231 731233 731242 731245 731252 731254 731256 731260 731340 731344 731346 731350 731354 731357 731362 731365 731368 731372 731375 731378 731381 731383 731388 731392 731394 731397 731398 731399 731606 731610 731615 731624 731630 731856 731857 731940 732160 732161 732163 732180 732992 733041 733149 733295 733358 733373 733374 733378 733606 733607 733617 733619 733739 733741 733745 733749 733754 733758 733877 734014 734079 734080 734081 734083 734084 734085 734086 734087 734089 734091 734328 734329 734330 734331 734333 734334 734335 734336 734433 734449 734450 734453 734456 735154 735166 735167 735324 735453 735874 735875 735876 735877 735880 735881 735883 736050 736172 737497 737610 738225 738329 738375 738376 738377 738378 738380 739048 739095 739101 739102 739104 739671 739768 739814 739826 739875 739883 739884 740162 740488 740532 740548 740550 740551 740552 740604 741247 741248 741249 741250 741251 741273 741275 741322 741324 741839 741928 741965 741966 741967 741997 741998 741999 742002 742004 742007 742564 742648 742684 743286 743288 743289 743292 743293 743295 743298 743401 743402 743452 743475 744019 744021 744024 744028 744030 744088 744120 744181 744183 744185 744188 744191 744756 744839 745478 745482 745484 745487 745528 745559 746214 746248 746687 746688 746689 747412 747688 748126 748137 748383 748408 748868 748878 748888 749110 749128 749834 749837 749838 749839 749840 749842 749843 749844 749845 749846 749847 749848 751062 751726 752512 754683 757484 759017 760464 761762 762629 763350 764071 766233 767564 767674 771276 771842 774158 774879 778481 781964 784968 790020 790561 792193 792951 792954 792959 793648 793661 795602 796557 796561 796569 796585 796598 796610 796635 796656 796675 796683 796700 797277 797434 799964 800331 801328 802509 802774 803966 803981 803986 803991 804722 804939 805448 806179 806184 806196 806405 806927 807102 807104 807825 808378 809829 809834 809842 809849 809861 809871 809880 809883 809888 809890 811334 811336 811337 811344 811349 811425 812079 812084 812086 812809 812811 813540 814322 814992 816528 818605 820065 820773 821493 823731 824370 825084 825086 825781 825792 825796 825799 825800 825803 825842 826570 828002 829427 830882 833042 835202 835256 838811 840950 843179 846782 848873 848944 851828 853913 859755 860416 861113 864713 869033 869843 875510 877701 878428 879148 880549 880654 881307 882746 884182 884184 884903 886420 889186 892907 897101 898538 898539 899255 900682 900690 902822 904437 904958 906370 909224 910925 912080 913092 914947 915262 915271 915276 915296 915317 916364 916783 917531 917557 917569 917590 917761 917782 918328 918342 919079 919087 919096 919103 919107 919111 919114 919184 919840 920567 920572 920584 920595 920605 920616 ];
L = superpixels(X,4708,'IsInputLab',true);
% Convert L*a*b* range to [0 1]
scaledX = prepLab(X);
BW = lazysnapping(scaledX,L,foregroundInd,backgroundInd);
% Close mask with line
length = 16.000000;
angle = 0.000000;
se = strel('line', length, angle);
BW = imclose(BW, se);
%% Blob Analysis
hblob = vision.BlobAnalysis('MinimumBlobArea',200,'MaximumBlobArea',5000);
[objarea,objcent,bboxOut]= hblob(BW)
% Annotate images
Image = insertShape(vidframe, 'rectangle', bboxOut);
% numObj = length(objarea);
% Image = insertText(Image, [20 20], int32(numObj), 'TextColor', [255 255 0], 'FontSize', 30);
% Display annotated frame
step(vidPlayer, Image);
end
% Clean up
release(v);
release(hblob);
function out = prepLab(in)
% Convert L*a*b* image to range [0,1]
out = in;
out(:,:,1) = in(:,:,1) / 100; % L range is [0 100].
out(:,:,2) = (in(:,:,2) + 86.1827) / 184.4170; % a* range is [-86.1827,98.2343].
out(:,:,3) = (in(:,:,3) + 107.8602) / 202.3382; % b* range is [-107.8602,94.4780].
end

Accepted Answer

VINAYAK LUHA
VINAYAK LUHA on 18 Jul 2024
Hi Shaherbano,
I understand that -
  1. You have video featuring a checkerboard with some green and red cross markings and,
  2. You are using "blob analysis" in MATLAB to calculate the distance of these cross markings from the central blue cross marking.
  3. However, you are encountering difficulties in removing the unwanted white areas.
Follow the below steps to remove the white areas -
  • Use the MATLAB "Color Segementer App" to select an appropriate range for the HSV threshold for the colours. I found the following thresholds for green, red, and blue colors to work effectively in this case:
%HSV thresholds for green, red, and blue colors in [Low, High] format
greenThreshold = [0.237 0.505; 0.082 1; 0.0 0.765];
redThreshold = [0.9 1; 0.25 1; 0.25 1];
blueThreshold = [0.5 0.7; 0.19 0.85; 0 1];
  • Additionally, you can impose an area threshold, say, 150px² on the detected "regionprops" to filter out the small, irrelevant regions.
Following are the masks obtained using the above outlined steps
Next, you can proceed with calculating and showing the distances between the red,green cross markings and the blue cross marking, you may use the MATLAB 'pdist2' function for calculating the distance followed by the "text" and "line" functions for showing the distance.
Here is the final result based on the above suggestions:
For more details on the functions you may use to implement your workflow, refer to the following MATLAB documentation.
I hope you can incorporate these suggestions into your code and extend it to calculating and showing distances in videos.
Regards,
Vinayak

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!