Extract features from training images (Matlab)











up vote
-1
down vote

favorite












Thanks for reading my question. Sorry if it's an easy question but I'm new in Matlab and Computer Vision. I'm trying to extract features from training images. I need to use histogram of color, histogram of gradient, SIFT-like features and compare their performance. I'm lost and have spent days to find a good way to use. How would you extract features in this example? I have put %Start and %End where I need to do that.



A written code would be greatly appreciated.



% Step-1: Load training and test data using imageSet.
imageDir = fullfile('./data');

% imageSet recursively scans the directory tree containing the images.
flowerImageSet = imageSet(imageDir, 'recursive');


%% step-2: Partition the data set into a training set and a test set.

tmparr=randperm(80);
arrTrainID=tmparr(1:50); %training ID;
arrTestID=tmparr(51:end);% testing ID;


%% Step-3: Train the classifier using features extracted from the training set.

% Step 3.1 Extract features from training images

trainingFeatures = ;
trainingLabels = ;
featureSize =128;
for flower = 1:numel(flowerImageSet)

numImages = numel(arrTrainID);
features = zeros(numImages, featureSize);

for i = 1:numImages

img = rgb2gray(read(flowerImageSet(flower), arrTrainID(i)));

%START
features(i, :) = rand(1,featureSize);
%END
end

% Use the imageSet Description as the training labels.
labels = repmat(flowerImageSet(flower).Description, numImages, 1);

trainingFeatures = [trainingFeatures; features];
trainingLabels = [trainingLabels; labels ];

end

%%
% Step 3.2, train a classifier using the extracted features.

classifier = fitcecoc(trainingFeatures, trainingLabels);

%% Step 4: Evaluate the Flower Classifier

testFeatures=;
testLabels=;
% Step 4.1: Loop over the testing images and extract features from each image.
for flower = 1:numel(flowerImageSet)

numImages = numel(arrTestID); %
features = zeros(numImages, featureSize);
for i = 1:numImages
img = rgb2gray(read(flowerImageSet(flower), arrTestID(i)));
%Start
features(i, :) = rand(1,featureSize);
%End
end

% Use the imageSet Description as the training labels.
labels = repmat(flowerImageSet(flower).Description, numImages, 1);
testFeatures = [testFeatures; features];
testLabels = [testLabels; labels ];
end

% Step 4.2: Make class predictions using the test features.
predictedLabels = predict(classifier, testFeatures);

% Step 4.3: Tabulate the results using a confusion matrix.
confMat = confusionmat(testLabels, predictedLabels);

% Step 4.4: calculate accuracy
fprintf('accuracy=%f',sum(diag(confMat))/sum(confMat(:)));









share|improve this question







New contributor




Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    -1
    down vote

    favorite












    Thanks for reading my question. Sorry if it's an easy question but I'm new in Matlab and Computer Vision. I'm trying to extract features from training images. I need to use histogram of color, histogram of gradient, SIFT-like features and compare their performance. I'm lost and have spent days to find a good way to use. How would you extract features in this example? I have put %Start and %End where I need to do that.



    A written code would be greatly appreciated.



    % Step-1: Load training and test data using imageSet.
    imageDir = fullfile('./data');

    % imageSet recursively scans the directory tree containing the images.
    flowerImageSet = imageSet(imageDir, 'recursive');


    %% step-2: Partition the data set into a training set and a test set.

    tmparr=randperm(80);
    arrTrainID=tmparr(1:50); %training ID;
    arrTestID=tmparr(51:end);% testing ID;


    %% Step-3: Train the classifier using features extracted from the training set.

    % Step 3.1 Extract features from training images

    trainingFeatures = ;
    trainingLabels = ;
    featureSize =128;
    for flower = 1:numel(flowerImageSet)

    numImages = numel(arrTrainID);
    features = zeros(numImages, featureSize);

    for i = 1:numImages

    img = rgb2gray(read(flowerImageSet(flower), arrTrainID(i)));

    %START
    features(i, :) = rand(1,featureSize);
    %END
    end

    % Use the imageSet Description as the training labels.
    labels = repmat(flowerImageSet(flower).Description, numImages, 1);

    trainingFeatures = [trainingFeatures; features];
    trainingLabels = [trainingLabels; labels ];

    end

    %%
    % Step 3.2, train a classifier using the extracted features.

    classifier = fitcecoc(trainingFeatures, trainingLabels);

    %% Step 4: Evaluate the Flower Classifier

    testFeatures=;
    testLabels=;
    % Step 4.1: Loop over the testing images and extract features from each image.
    for flower = 1:numel(flowerImageSet)

    numImages = numel(arrTestID); %
    features = zeros(numImages, featureSize);
    for i = 1:numImages
    img = rgb2gray(read(flowerImageSet(flower), arrTestID(i)));
    %Start
    features(i, :) = rand(1,featureSize);
    %End
    end

    % Use the imageSet Description as the training labels.
    labels = repmat(flowerImageSet(flower).Description, numImages, 1);
    testFeatures = [testFeatures; features];
    testLabels = [testLabels; labels ];
    end

    % Step 4.2: Make class predictions using the test features.
    predictedLabels = predict(classifier, testFeatures);

    % Step 4.3: Tabulate the results using a confusion matrix.
    confMat = confusionmat(testLabels, predictedLabels);

    % Step 4.4: calculate accuracy
    fprintf('accuracy=%f',sum(diag(confMat))/sum(confMat(:)));









    share|improve this question







    New contributor




    Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      Thanks for reading my question. Sorry if it's an easy question but I'm new in Matlab and Computer Vision. I'm trying to extract features from training images. I need to use histogram of color, histogram of gradient, SIFT-like features and compare their performance. I'm lost and have spent days to find a good way to use. How would you extract features in this example? I have put %Start and %End where I need to do that.



      A written code would be greatly appreciated.



      % Step-1: Load training and test data using imageSet.
      imageDir = fullfile('./data');

      % imageSet recursively scans the directory tree containing the images.
      flowerImageSet = imageSet(imageDir, 'recursive');


      %% step-2: Partition the data set into a training set and a test set.

      tmparr=randperm(80);
      arrTrainID=tmparr(1:50); %training ID;
      arrTestID=tmparr(51:end);% testing ID;


      %% Step-3: Train the classifier using features extracted from the training set.

      % Step 3.1 Extract features from training images

      trainingFeatures = ;
      trainingLabels = ;
      featureSize =128;
      for flower = 1:numel(flowerImageSet)

      numImages = numel(arrTrainID);
      features = zeros(numImages, featureSize);

      for i = 1:numImages

      img = rgb2gray(read(flowerImageSet(flower), arrTrainID(i)));

      %START
      features(i, :) = rand(1,featureSize);
      %END
      end

      % Use the imageSet Description as the training labels.
      labels = repmat(flowerImageSet(flower).Description, numImages, 1);

      trainingFeatures = [trainingFeatures; features];
      trainingLabels = [trainingLabels; labels ];

      end

      %%
      % Step 3.2, train a classifier using the extracted features.

      classifier = fitcecoc(trainingFeatures, trainingLabels);

      %% Step 4: Evaluate the Flower Classifier

      testFeatures=;
      testLabels=;
      % Step 4.1: Loop over the testing images and extract features from each image.
      for flower = 1:numel(flowerImageSet)

      numImages = numel(arrTestID); %
      features = zeros(numImages, featureSize);
      for i = 1:numImages
      img = rgb2gray(read(flowerImageSet(flower), arrTestID(i)));
      %Start
      features(i, :) = rand(1,featureSize);
      %End
      end

      % Use the imageSet Description as the training labels.
      labels = repmat(flowerImageSet(flower).Description, numImages, 1);
      testFeatures = [testFeatures; features];
      testLabels = [testLabels; labels ];
      end

      % Step 4.2: Make class predictions using the test features.
      predictedLabels = predict(classifier, testFeatures);

      % Step 4.3: Tabulate the results using a confusion matrix.
      confMat = confusionmat(testLabels, predictedLabels);

      % Step 4.4: calculate accuracy
      fprintf('accuracy=%f',sum(diag(confMat))/sum(confMat(:)));









      share|improve this question







      New contributor




      Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Thanks for reading my question. Sorry if it's an easy question but I'm new in Matlab and Computer Vision. I'm trying to extract features from training images. I need to use histogram of color, histogram of gradient, SIFT-like features and compare their performance. I'm lost and have spent days to find a good way to use. How would you extract features in this example? I have put %Start and %End where I need to do that.



      A written code would be greatly appreciated.



      % Step-1: Load training and test data using imageSet.
      imageDir = fullfile('./data');

      % imageSet recursively scans the directory tree containing the images.
      flowerImageSet = imageSet(imageDir, 'recursive');


      %% step-2: Partition the data set into a training set and a test set.

      tmparr=randperm(80);
      arrTrainID=tmparr(1:50); %training ID;
      arrTestID=tmparr(51:end);% testing ID;


      %% Step-3: Train the classifier using features extracted from the training set.

      % Step 3.1 Extract features from training images

      trainingFeatures = ;
      trainingLabels = ;
      featureSize =128;
      for flower = 1:numel(flowerImageSet)

      numImages = numel(arrTrainID);
      features = zeros(numImages, featureSize);

      for i = 1:numImages

      img = rgb2gray(read(flowerImageSet(flower), arrTrainID(i)));

      %START
      features(i, :) = rand(1,featureSize);
      %END
      end

      % Use the imageSet Description as the training labels.
      labels = repmat(flowerImageSet(flower).Description, numImages, 1);

      trainingFeatures = [trainingFeatures; features];
      trainingLabels = [trainingLabels; labels ];

      end

      %%
      % Step 3.2, train a classifier using the extracted features.

      classifier = fitcecoc(trainingFeatures, trainingLabels);

      %% Step 4: Evaluate the Flower Classifier

      testFeatures=;
      testLabels=;
      % Step 4.1: Loop over the testing images and extract features from each image.
      for flower = 1:numel(flowerImageSet)

      numImages = numel(arrTestID); %
      features = zeros(numImages, featureSize);
      for i = 1:numImages
      img = rgb2gray(read(flowerImageSet(flower), arrTestID(i)));
      %Start
      features(i, :) = rand(1,featureSize);
      %End
      end

      % Use the imageSet Description as the training labels.
      labels = repmat(flowerImageSet(flower).Description, numImages, 1);
      testFeatures = [testFeatures; features];
      testLabels = [testLabels; labels ];
      end

      % Step 4.2: Make class predictions using the test features.
      predictedLabels = predict(classifier, testFeatures);

      % Step 4.3: Tabulate the results using a confusion matrix.
      confMat = confusionmat(testLabels, predictedLabels);

      % Step 4.4: calculate accuracy
      fprintf('accuracy=%f',sum(diag(confMat))/sum(confMat(:)));






      matlab






      share|improve this question







      New contributor




      Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Travis

      1




      1




      New contributor




      Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Travis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Travis is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402693%2fextract-features-from-training-images-matlab%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Travis is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Travis is a new contributor. Be nice, and check out our Code of Conduct.













          Travis is a new contributor. Be nice, and check out our Code of Conduct.












          Travis is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402693%2fextract-features-from-training-images-matlab%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

          Sphinx de Gizeh